Unity 根据Transform、GameObject和Tag获取子对象集合
来源:互联网 发布:施工图网络字母缩写 编辑:程序博客网 时间:2024/05/22 22:08
因为项目中难免要多次进行获取子对象或者子对象的集合,所以同事之前写了一个单独的类,用来做这些操作。然后再实际的项目中,只需要使用 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; } }
0 0
- Unity 根据Transform、GameObject和Tag获取子对象集合
- Unity3D 06-根据Transform、GameObject和Tag获取子对象集合
- Unity 中的gameObject 和 transform
- unity gameobject transform
- 【技术】【总结】Unity:理解Gameobject ,gameObject,Transform,transform的区别和关联
- 图解unity下gameobject和transform的区别和关联
- Unity中直接使用transform和gameObject的效率测试
- 【Unity】获得当前脚本的物体的transform和gameobject
- u3d transform 和 gameobject
- GameObject和Transform的区别
- Unity中使用GameObject.Find、Transform.Find查找GameObject
- GameObject的隐藏子对象获取的方法
- Unity3D获取GameObject的所有子对象,添加特定标签
- GameObject.Find()、Transform.Find查找游戏对象
- Unity获取GameObject单例
- childObject.transform.parent.gameObject Unity得到gameObject后,取GameObject的父
- Unity GameObject.activeSelf, GameObject.activeInHierarchy,GameObject.SetActive和SetActiveRecursively
- Unity GameObject.activeSelf, GameObject.activeInHierarchy,GameObject.SetActive和SetActiveRecursively
- (初学)android中TabHost原理详解及实例
- 大家怎看中港冲突?
- 动态SQL
- Oracle 解除对SCOTT/TIGER用户的锁定。
- eclipse tomcat 的内存设置
- Unity 根据Transform、GameObject和Tag获取子对象集合
- jQuery's jquery-1.10.2.min.map is triggering a 404 (Not Found)
- Linux下进程信息的深入分析
- Java学习的30个目标以及系统架构师推荐的书
- 【极客学院】点九在Android平台的运用开发,实现屏幕的完整适配
- 使用 SysRq 键安全重启挂起的 Linux
- GeoServer(2)--使用Eclipse安装GeoServer
- Sql Server排序规则(转)
- iOS - 获取文件MD5 :