Unity 3D之進階篇Transform的叠代器
阿新 • • 發佈:2017-08-02
number iss spa ict 遍歷 com blog rotation 是我
說在前面
我們通常使用Transform作為3D物體的一個位置屬性來看待,從這個類中獲取或者控制,物體的大小、位置、縮放值,或者通過transform來找到物體的子節點。but!!Transform繼承了IEnumberable,是可以使用foreach遍歷的。
Transform
// // 摘要: // /// // Position, rotation and scale of an object. // /// public class Transform : Component, IEnumerable
首先獲取unity裏面對Transform類的定義。
Transform類繼承自component類,並且繼承IEnumerable的接口。
繼承IEnumerable的對象我們知道是可以使用叠代器foreach來遍歷的。
通常使用foreach的都是list、dictionary等耳熟能詳的容器,那麽和transform有什麽關系呢?
Transform的叠代器
public class TestTransFind : MonoBehaviour { public Transform parent; void Start () { Debug.Log(parent.transform.name);foreach (Transform item in parent.transform) { Debug.Log(item.name); } } }//end_class
將腳本掛在一個物體上,然後將canvas賦值給parent,
運行後輸出:
可以看到在輸出後將parent的所有子物體都輸出出來了。
結論
我們不知道getEnumberator內部是怎麽實現的,但是我們知道,可以使用foreach來獲取到transform的所有子對象。
THISSKY出品,轉載請註明:http://www.cnblogs.com/zhuhongjongy/p/7275957.html
Unity 3D之進階篇Transform的叠代器