U3D技术采集Unity基础

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

2017-03-22  本文已影响11人  游戏开发小Y
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);
            }
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读