• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

常州微信小程序游戏开发-U3D查找子物体

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0110/325.html

常州微信小程序游戏开发-Unity3D通过GameObject查找子物体

 

游戏脚本中查找物体的方式有很多种,下面要和大家介绍的这种查找子物体的方式是通过GameObject来实现的,不了解的可以看看。

 

在给大家介绍查找子物体之前,我们要明白什么是 GameObjectUnity 中每一个存在的对象,即为一个 GameObject,包括空物体,同样是;并且无论激活与否。

 

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);

           

       

   



上篇:上一篇:常州手游开发培训-U3D所有特殊的文件夹
下篇:下一篇:常州手游开发设计-U3D占用内存太大解决