2015-06-13 143 views
0

我有這塊代碼。我需要迭代每個子對象,但是子對象可以有更多的子對象。當程序第一次運行時調用Awake()方法。所以,我在問如何構建一個迭代器來檢查孩子內是否有孩子。然後對這些孩子運行ResizeMeshCollection(MeshFilter[] collection),直到不再有子對象。迭代通過多級子對象

public class ResizeAsset : MonoBehaviour { 


    void Awake() 
    { 
     ResizeMeshCollection(this.GetComponentsInChildren<MeshFilter>()); 
    } 

    void ResizeMeshCollection(MeshFilter[] collection) 
    { 
     foreach (MeshFilter mf in collection) 
     { 
      mf.GetComponent<MeshFilter>(); 
      Transform tf = mf.GetComponent<Transform>(); 
      /// Resize logic here 
      /// check if there are children under this child object, 
      /// Run ResizeMeshCollection() on 
      /// all children until there are no more. 

我敢肯定,我不想在自身運行ResizeMeshCollection(),我也不知道這是可能的,但我只是無法弄清楚如何做到這一點。

+0

標籤[tag:unity]的第一個標記是「不要在關於UNITY GAME ENGINE的問題上使用(使用:[tag:unity3d]來代替)」 –

+1

http://www.hacker-dictionary.com/條款/遞歸 –

+0

那麼,有沒有這樣做?我知道調用內部方法可能會產生一些不良影響 –

回答

1

你寫它的方式其實就是你想要做到的。該方法被稱爲遞歸。這是一種非常流行的方式來實現你正在嘗試做的事情,沒有錯誤的方法調用自己,如果在某些準則中完成,例如在再次執行自己之前符合某些標準。