unity遍歷場景對象下所有對象的腳本,並打印路徑

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	[ContextMenu("TextHave")]
    public void GetUITextHave()
    {

        int length = GetComponentsInChildren<UILabel>(true).Length;
//true代表隱藏的物體也會被遍歷
        string path;
        Transform temp;
        for (int i = 0; i < length; i++)
        {
           
            temp = GetComponentsInChildren<UILabel>(true)[i].transform;
            path = temp.name + "/";
            while (temp.parent != null)
            {
                temp = temp.parent;
                path += temp.name + "/";
                // Debug.Log(1);
            }
            
            Debug.Log(GetComponentsInChildren<UILabel>(true)[i].text + "  " + path);

         

        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章