Unity3D 06-根据Transform、GameObject和Tag获取子对象集合
来源:互联网 发布:淘宝如何数据选子类目 编辑:程序博客网 时间:2024/06/15 18:12
导引:
因为项目中难免要多次进行获取子对象或者子对象的集合,所以写一个单独的类,用来做这些操作。然后再实际的项目中,只需要使用 transform 或者 gameobject 调用这些方法就可以快速的得到这些数据,而并不需要自己在每个单独的类里面都写上一遍。
代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; public static partial class ExtentionMethod { /// <summary> /// 获取子对象变换集合 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static List<Transform> GetChildCollection(this Transform obj) { List<Transform> list = new List<Transform>(); for (int i = 0; i < obj.childCount; i++) { list.Add(obj.GetChild(i)); } return list; } /// <summary> /// 获取子对象集合 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static List<GameObject> GetChildCollection(this GameObject obj) { var list = obj.transform.GetChildCollection(); return list.ConvertAll(T => T.gameObject); } public static Transform GetRootParent(this Transform obj) { Transform Root = obj.parent; while(Root.parent != null) { //Root = Root.root; //transform.root,方法可以直接获取最上父节点。 Root = Root.parent; } return Root; } /// <summary> /// 把源对象身上的所有组件,添加到目标对象身上 /// </summary> /// <param name="origin">源对象</param> /// <param name="target">目标对象</param> public static void CopyComponent(GameObject origin, GameObject target) { var originComs = origin.GetComponents<Component>(); foreach (var item in originComs) { target.AddComponent(item.GetType()); } } /// <summary> /// 改变游戏脚本 /// </summary> /// <param name="origin"></param> /// <param name="target"></param> public static void ChangeScriptTo(this MonoBehaviour origin, MonoBehaviour target) { target.enabled = true; origin.enabled = false; } /// <summary> /// 从当前对象的子对象中查找,返回一个用tag做标识的活动的游戏物体的链表.如果没有找到则为空. /// </summary> /// <param name="obj">对象Transform</param> /// <param name="tag">标签</param> /// <param name="transList">结果Transform集合</param> // 对一个父对象进行递归遍历,如果有子对象的tag和给定tag相符合时,则把该子对象存到 链表数组中 public static void FindGameObjectsWithTagRecursive(this Transform obj, string tag, ref List<Transform> transList) { foreach (var item in obj.transform.GetChildCollection()) { // 如果子对象还有子对象,则再对子对象的子对象进行递归遍历 if (item.childCount > 0) { item.FindGameObjectsWithTagRecursive(tag, ref transList); } if (item.tag == tag) { transList.Add(item); } } } public static void FindGameObjectsWithTagRecursive(this GameObject obj, string tag, ref List<GameObject> objList) { List<Transform> list = new List<Transform>(); obj.transform.FindGameObjectsWithTagRecursive(tag, ref list); objList.AddRange(list.ConvertAll(T => T.gameObject)); } /// <summary> /// 从父对象中查找组件 /// </summary> /// <typeparam name="T">组件类型</typeparam> /// <param name="com">物体组件</param> /// <param name="parentLevel">向上查找的级别,使用 1 表示与本对象最近的一个级别</param> /// <param name="searchDepth">查找深度</param> /// <returns>查找成功返回相应组件对象,否则返回null</returns> public static T GetComponentInParent<T>(this Component com, int parentLevel = 1, int searchDepth = int.MaxValue) where T : Component { searchDepth--; if (com != null && searchDepth > 0) { var component = com.transform.parent.GetComponent<T>(); if (component != null) { parentLevel--; if (parentLevel == 0) { return component; } } return com.transform.parent.GetComponentInParent<T>(parentLevel, searchDepth); } return null; } }
补充:Unity中三种调用其他脚本函数的方法
第一种:被调用脚本函数为static类型,调用时直接用 脚本名.函数名()。很不实用~
第二种:GameObject.Find(“脚本所在物体名”).SendMessage(“函数名”); 此种方法可以调用public和private类型函数
第三种:GameObject.Find(“脚本所在物体名”).GetComponent<脚本名>().函数名();此种方法只可以调用public类型函数
阅读全文
1 0
- Unity3D 06-根据Transform、GameObject和Tag获取子对象集合
- Unity 根据Transform、GameObject和Tag获取子对象集合
- Unity3D获取GameObject的所有子对象,添加特定标签
- 【Unity3D游戏开发】GameObject.Find()、Transform.Find查找隐藏对象
- unity3D中GameObject.Find()、Transform.Find查找隐藏对象分析
- u3d transform 和 gameobject
- GameObject和Transform的区别
- Unity 中的gameObject 和 transform
- 【Unity3D游戏开发】GameObject.Find()、Transform.Find查找隐藏对象 (十)
- 【Unity3D游戏开发】GameObject.Find()、Transform.Find查找隐藏对象 (十)
- C# 对象和Unity3d GameObject缓冲对象池模版类
- GameObject的隐藏子对象获取的方法
- unity3d 查找GameObject对象,FindObjectsOfType和FindObjectOfType 之遍历
- Unity3D获取两个GameObject间距
- unity3d transform基本属性获取和设置
- GameObject.Find()、Transform.Find查找游戏对象
- GameObject.Find()和transform.Find()混合使用
- Unity3D脚本:隐藏GameObject子物体
- 内部类
- Spring MVC+ Ireport 做统计报表,导出HTML、EXCEL等格式的文件,打印;
- opencv编译安装中出现 file INSTALL cannot set permissions on这种问题
- TextView基础
- MySQL TIMESTAMP相关问题
- Unity3D 06-根据Transform、GameObject和Tag获取子对象集合
- 关于STM32软件复位代码编写 (转)
- 9.7-全栈Java笔记:Set接口和实现类
- 【Linux】实现睡眠函数mysleep
- 使用开源软件快速搭建数据分析平台
- spark python 机器学习
- RecyclerView使用介绍——企业实用
- VS快捷键及技巧
- [Algorithm]九章九之二:Sequence