9

私は 3 つの子ゲーム オブジェクトを持っています (下の図でRedPink & Blueでわかるように)。これらは、親ゲーム オブジェクト Green の子です。

Parent(Green) GameObjectのサイズを計算する方法がわかりません。

このコードを使用して、実行時にこれらすべてのゲーム オブジェクトを作成しています。

GameObject CreatePattern(int difficultyLevel)    
    {    
        GameObject gameObjectTemp = new GameObject();    
        SinglePattern singlePattern;    
        gameObjectTemp = (GameObject) Instantiate(gameObjectTemp);   


        singlePattern = freeRunDataHandler.GetSinglePatternRandom(1);    
        GameObject gameObjectSingleObject = null;    
        foreach (SingleObject singleObject in singlePattern.singleObjectList)    
        {
                gameObjectSingleObject = GetGameObjectByCategory(singleObject.catergory, singleObject.type);

            if (gameObjectSingleObject != null)    
            {    
                gameObjectSingleObject = (GameObject) Instantiate(gameObjectSingleObject, new Vector3(singleObject.positionX, singleObject.positionY, singleObject.positionZ), Quaternion.identity);    
                gameObjectSingleObject.transform.localScale = new Vector3(singleObject.length, 1, singleObject.width);    
                gameObjectSingleObject.transform.parent = gameObjectTemp.transform;    
            }    
        }        

        return gameObjectTemp;    
    }

この関数は、すべての子を追加した後、親 (緑) の gameObject を返します。私の親(緑)には、コンポーネント(BoxCollider、MeshFilter、MeshRendererなど)さえも何も添付されていません。

BoxCollider、MeshRenderer、MeshFilter(テスト用)を添付し、親で試しました:

parent.collider.bounds.size.x  ----- > box collider
parent.renderer.bounds.size.x  ----- > mesh renderer

しかし、何も機能しません。場合によっては 1 または 0 が返されます。Parent(Green) GameObject のサイズを取得する方法を教えてください。

ここに画像の説明を入力

4

2 に答える 2

30

サイズとは境界を意味しますか?もしそうなら、それはあなたが作ったよりもずっと簡単なはずです。私のコードはテストされていません。Unity は手元にありませんが、これは機能するはずです。「親」は階層内のゲーム オブジェクトであり、階層内でその下に「子」があると想定しています。

Bounds bounds = parent.renderer.bounds;
foreach (Transform child in parent.transform)
{
    bounds.encapsulate(child.gameObject.renderer.bounds);
}

アップデート:

または、レンダラーを持つ親が必要ない場合:

// First find a center for your bounds.
Vector3 center = Vector3.zero;

foreach (Transform child in parent.transform)
{
    center += child.gameObject.renderer.bounds.center;   
}
center /= parent.transform.childCount; //center is average center of children

//Now you have a center, calculate the bounds by creating a zero sized 'Bounds', 
Bounds bounds = new Bounds(center,Vector3.zero); 

foreach (Transform child in parent.transform)
{
    bounds.encapsulate(child.gameObject.renderer.bounds);   
}

親/子階層を使用したくないとおっしゃいました。そのルートに行かない場合は、「子」をある種の配列に追加するか、GameObject.Find() メソッドを使用して各子を名前で検索する必要があります。「child_1」、「child_2」などの名前を付けると、かなり簡単に調べることができますが、単純に親オブジェクトを作成する方がはるかに簡単です。

于 2012-08-14T21:21:41.567 に答える
1

これは古い投稿ですが、同様のニーズがあり、いくつかの問題が原因でいくつかの問題に遭遇しました

1つ目は、計算された親ボックスが本来よりも広く、中央に正しく配置されていないことに気付きました

原因は、オブジェクトに回転が適用されていて、境界が適用されなかったためです。そのため、計算を実行する前に、最初にゼロに戻す必要がありました。計算したら、元の回転を復元できました。

2 つ目は、私の場合、すべての子オブジェクトにレンダラーがあるわけではありません。また、私の子供には独自の子がある可能性があるため、変換の子を中心に計算したくありません。子のレンダリング コンポーネントが必要でした。

以下は、私が思いついた結果のコードです。重いコメント; 最適化されていませんが、トリックを行い、必要に応じて孫を考慮し、開始時に回転するオブジェクトを処理します。

        //Store our current rotation
        Vector3 LocalAngle = transform.localEulerAngles;
        //Zero the rotation before we calculate as bounds are never rotated
        transform.localEulerAngles = Vector3.zero;
        //Establish a default center location
        Vector3 center = Vector3.zero;
        //Establish a default empty bound
        occupiedSpace = new Bounds (Vector3.zero, Vector3.zero);
        //Count the children with renderers
        int count = 0;
        //We only count childrent with renderers which should be fine as the bounds center is global space
        foreach (Renderer render in transform.GetComponentsInChildren<Renderer>()) {
            center += render.bounds.center;  
            count++;
        }
        //Return the average center assuming we have any renderers
        if(count > 0)
            center /= count;
        //Update the parent bound accordingly
        occupiedSpace.center = center;
        //Again for each and only after updating the center expand via encapsulate
        foreach (Renderer render in transform.GetComponentsInChildren<Renderer>()) {
            occupiedSpace.Encapsulate(render.bounds);
        }
        //In by case I want to update the parents box collider so first I need to bring things into local space
        occupiedSpace.center -= transform.position;
        boxCollider.center = occupiedSpace.center;
        boxCollider.size = occupiedSpace.size;
        //Restore our original rotation
        transform.localEulerAngles = LocalAngle;
于 2014-04-09T23:47:58.717 に答える