在开发过程中用到最多的就是查找子物体或者子物体上的组件了,由于不能一一将组件或者子物体拖到脚本上。所以就需要查找方法,Unity的Transform提供的方法只能查找一级下的子物体。座椅就有了今天我说的方法。
主要用到了递归方法,虽然这个方法很耗时,但是目前也没有什么方法可以代替这个方法,下面就直接上代码。文章源自大腿Plus-https://www.zhaoshijun.com/archives/1002
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
using UnityEngine; using System.Collections; using UnityEngine.UI; public static class Utilities { public static T GetChild<T>(Transform parent, string childName, UnityEngine.Events.UnityAction BtnClickHandle = null) where T : Component { T t = null; if (parent.Find(childName) == null) { for (int i = 0; i < parent.childCount; i++) { t = GetChild<T>(parent.GetChild(i), childName); if (t != null) break; } } else { GameObject obj = parent.Find(childName).gameObject; t = obj.GetComponent<T>(); } if (t != null && t is Button && BtnClickHandle != null) { (t as Button).onClick.RemoveListener(BtnClickHandle); (t as Button).onClick.AddListener(BtnClickHandle); } return t; } public static T GetChild<T>(GameObject parent, string childName, UnityEngine.Events.UnityAction BtnClickHandle = null) where T : Component { T t = null; if (parent.transform.Find(childName) == null) { for (int i = 0; i< parent.transform.childCount; i++) { t = GetChild<T>(parent.transform.GetChild(i), childName); if (t != null) break; } } else { GameObject obj = parent.transform.Find(childName).gameObject; t = obj.GetComponent<T>(); } if (t != null && t is Button && BtnClickHandle != null) { (t as Button).onClick.RemoveListener(BtnClickHandle); (t as Button).onClick.AddListener(BtnClickHandle); } return t; } /// <summary> /// 查找本游戏物体下的特定名称的子物体系统,并将其返回 /// </summary> /// <param name="_target">要在其中进行查找的父物体</param> /// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param> /// <returns></returns> public static Transform FindDeepChild(this GameObject _target, string _childName) { Transform resultTrs = null; resultTrs = _target.transform.Find(_childName); if (resultTrs == null) { foreach (Transform trs in _target.transform) { resultTrs = trs.gameObject.FindDeepChild(_childName); if (resultTrs != null) return resultTrs; } } return resultTrs; } /// <summary> /// 查找本游戏物体下的特定名称的子物体系统,并将其返回 /// </summary> /// <param name="_target">要在其中进行查找的父物体</param> /// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param> /// <returns></returns> public static Transform FindDeepChild(this Transform _target, string _childName) { Transform resultTrs = null; resultTrs = _target.Find(_childName); if (resultTrs == null) { foreach (Transform trs in _target) { resultTrs = trs.gameObject.FindDeepChild(_childName); if (resultTrs != null) return resultTrs; } } return resultTrs; } /// <summary> /// 查找本游戏物体下的特定名称的子物体系统的特定组件,并将其返回 /// </summary> /// <param name="_target">要在其中进行查找的父物体</param> /// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param> /// <returns>返回找到的符合条件的第一个自物体下的指定组件</returns> public static T FindDeepChild<T>(this GameObject _target, string _childName) where T : Component { Transform resultTrs = _target.FindDeepChild(_childName); if (resultTrs != null) return resultTrs.gameObject.GetComponent<T>(); return (T)((object)null); } /// <summary> /// 查找本游戏物体下的特定名称的子物体系统的特定组件,并将其返回 /// </summary> /// <param name="_target">要在其中进行查找的父物体</param> /// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param> /// <returns>返回找到的符合条件的第一个自物体下的指定组件</returns> public static T FindDeepChild<T>(this Transform _target, string _childName) where T : Component { Transform resultTrs = _target.FindDeepChild(_childName); if (resultTrs != null) return resultTrs.gameObject.GetComponent<T>(); return (T)((object)null); } } |
这就是查找子物体及子物体组件的工具类,不管有多少层级,都可以查找到需要的东西。文章源自大腿Plus-https://www.zhaoshijun.com/archives/1002 文章源自大腿Plus-https://www.zhaoshijun.com/archives/1002
我的微信
微信扫一扫

shijun_z
我的QQ
QQ扫一扫

846207670
评论