美文网首页U3D技术采集Unity基础
Unity3D 高效率查找子物体(孩子比较多 需要查找的物体比

Unity3D 高效率查找子物体(孩子比较多 需要查找的物体比

作者: 游戏开发小Y | 来源:发表于2017-03-22 10:02 被阅读11次
    using System.Collections;
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;
    
    public class TestRes : MonoBehaviour {
    
        public GameObject go;
    
        // Use this for initialization
        void Start () {
    
            GameObject target = GameObject.Instantiate(go) as GameObject;
            List<Transform> findTest = new List<Transform>();
    
            string[] findNames = new string[]
                { "Directional light","GameObject (3)","GameObject (4)"};
    
            FindChild(target.transform, new List<string>(findNames), ref findTest);
    
            for (int i = 0; i < findTest.Count; i++)
            {
                if (findTest[i].name.Equals(findNames[0])) 
                {
                    Light li = findTest[i].GetComponent<Light>();
                    if (li != null)
                    {
                        li.color = Color.black;
                        Debug.Log(li.color);
                    }
                }
                else
                {
                    Debug.Log(findTest[i].name);
                }
    
    
            }
    
        }
    
        void FindChild(Transform go,List<string> findName, ref List<Transform> trf)
        {
            if (trf.Count == findName.Count)
            {
                return;
            }
            if (findName.Contains(go.name))
            {
                trf.Add(go);
            }
            if (go.childCount != 0)
            {
                for (int i = 0; i < go.childCount; i++)
                {
                    FindChild(go.GetChild(i), findName, ref trf);
                }
            }
        }
    }
    

    相关文章

      网友评论

        本文标题: Unity3D 高效率查找子物体(孩子比较多 需要查找的物体比

        本文链接:https://www.haomeiwen.com/subject/eiannttx.html