unity 获得所有的tag_Unity3D_06_根据Transform、GameObject和Tag获取子对象集合

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingUnityEngine;6

7

8 public static partial classExtentionMethod9 {10 ///

11 ///获取子对象变换集合12 ///

13 ///

14 ///

15 public static List GetChildCollection(thisTransform obj)16 {17 List list = new List();18 for (int i = 0; i < obj.childCount; i++)19 {20 list.Add(obj.GetChild(i));21 }22 returnlist;23 }24

25 ///

26 ///获取子对象集合27 ///

28 ///

29 ///

30 public static List GetChildCollection(thisGameObject obj)31 {32 var list =obj.transform.GetChildCollection();33 return list.ConvertAll(T =>T.gameObject);34 }35

36 public static Transform GetRootParent(thisTransform obj)37 {38 Transform Root =obj.parent;39 while(Root.parent != null)40 {41 //Root = Root.root;//transform.root,方法可以直接获取最上父节点。

42 Root =Root.parent;43 }44 returnRoot;45 }46

47 ///

48 ///把源对象身上的所有组件,添加到目标对象身上49 ///

50 /// 源对象

51 /// 目标对象

52 public static voidCopyComponent(GameObject origin, GameObject target)53 {54 var originComs = origin.GetComponents();55 foreach (var item inoriginComs)56 {57 target.AddComponent(item.GetType());58 }59 }60

61 ///

62 ///改变游戏脚本63 ///

64 ///

65 ///

66 public static void ChangeScriptTo(thisMonoBehaviour origin, MonoBehaviour target)67 {68 target.enabled = true;69 origin.enabled = false;70 }71

72

73 ///

74 ///从当前对象的子对象中查找,返回一个用tag做标识的活动的游戏物体的链表.如果没有找到则为空.75 ///

76 /// 对象Transform

77 /// 标签

78 /// 结果Transform集合// 对一个父对象进行递归遍历,如果有子对象的tag和给定tag相符合时,则把该子对象存到 链表数组中

79 public static void FindGameObjectsWithTagRecursive(this Transform obj, string tag, ref ListtransList)80 {81 foreach (var item inobj.transform.GetChildCollection())82 {83 //如果子对象还有子对象,则再对子对象的子对象进行递归遍历

84 if (item.childCount > 0)85 {86 item.FindGameObjectsWithTagRecursive(tag, reftransList);87 }88

89 if (item.tag ==tag)90 {91 transList.Add(item);92 }93 }94 }95

96 public static void FindGameObjectsWithTagRecursive(this GameObject obj, string tag, ref ListobjList)97 {98 List list = new List();99 obj.transform.FindGameObjectsWithTagRecursive(tag, reflist);100

101 objList.AddRange(list.ConvertAll(T =>T.gameObject));102 }103

104 ///

105 ///从父对象中查找组件106 ///

107 /// 组件类型

108 /// 物体组件

109 /// 向上查找的级别,使用 1 表示与本对象最近的一个级别

110 /// 查找深度

111 /// 查找成功返回相应组件对象,否则返回null

112 public static T GetComponentInParent(this Component com, int parentLevel = 1, int searchDepth = int.MaxValue) whereT : Component113 {114 searchDepth--;115

116 if (com != null && searchDepth > 0)117 {118 var component = com.transform.parent.GetComponent();119 if (component != null)120 {121 parentLevel--;122 if (parentLevel == 0)123 {124 returncomponent;125 }126 }127

128 return com.transform.parent.GetComponentInParent(parentLevel, searchDepth);129 }130

131 return null;132 }133 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值