Unity3D_06_根据Transform、GameObject和Tag获取子对象集合
导引:
因为项目中难免要多次进行获取子对象或者子对象的集合,所以写一个单独的类,用来做这些操作。然后再实际的项目中,只需要使用 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 = ; 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 > )
{
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 = , int searchDepth = int.MaxValue) where T : Component
{
searchDepth--; if (com != null && searchDepth > )
{
var component = com.transform.parent.GetComponent<T>();
if (component != null)
{
parentLevel--;
if (parentLevel == )
{
return component;
}
} return com.transform.parent.GetComponentInParent<T>(parentLevel, searchDepth);
} return null;
}
}
补充:Unity中三种调用其他脚本函数的方法
第一种:被调用脚本函数为static类型,调用时直接用 脚本名.函数名()。很不实用~
第二种:GameObject.Find(“脚本所在物体名”).SendMessage(“函数名”); 此种方法可以调用public和private类型函数
第三种:GameObject.Find(“脚本所在物体名”).GetComponent<脚本名>().函数名();此种方法只可以调用public类型函数
Unity3D_06_根据Transform、GameObject和Tag获取子对象集合的更多相关文章
- NX二次开发-UFUN由工程图视图tag获取图纸页tag UF_DRAW_ask_drawing_of_view
#include <uf.h> #include <uf_draw.h> #include <uf_drf.h> #include <uf_obj.h> ...
- NX二次开发-UFUN输入特征TAG,获取特征所有表达式TAG和个数UF_MODL_ask_exps_of_feature
NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建块 UF_FEATURE_SIGN S ...
- NX二次开发-UFUN输入Part的TAG,获取整个部件表达式的TAG和表达式个数UF_MODL_ask_exps_of_part
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_part.h> UF_initialize ...
- Atitit利用反射获取子类 集合 以及继承树
Atitit利用反射获取子类 集合 以及继承树 想从父类往下找子类的确是不可能的,要知道只要类不是final的话谁都有继承它的自由不需要事前通知父类. Eclipse实现不是重父类开始找而是重子类往回 ...
- Js获取后台集合List的值和下标的方法
Js获取后台集合List的值和下标的方法 转载自:http://blog.csdn.net/XiaoKanZheShiJie/article/details/47280449 首先用的是struts2 ...
- 从值栈获取List集合
-------------------siwuxie095 从值栈获取 List 集合 1.具体步骤 (1)在 Action 中向值栈放 List 集合 (2)在 JSP 页面中从值栈获取 List ...
- 反射方式,获取出集合ArrayList类的class文件对象
/* * 定义集合类,泛型String * 要求向集合中添加Integer类型 * * 反射方式,获取出集合ArrayList类的class文件对象 * 通过class文件对象,调用add方法 * * ...
- Java分享笔记:使用entrySet方法获取Map集合中的元素
/*--------------------------------- 使用entrySet方法取出Map集合中的元素: ....该方法是将Map集合中key与value的关系存入到了Set集合中,这 ...
- 获取单列集合,双列集合,数组的Stream流对象以及简单操作
获取流对象 获取单列集合,双列集合,数组的流对象 单列集合获取流对象: 1.java.util.Collection接口中加入了default方法stream()获取流对象,因此其所有实现类均可通过此 ...
随机推荐
- 程序员修神之路--用NOSql给高并发系统加速(送书)
随着互联网大潮的到来,越来越多网站,应用系统需要海量数据的支撑,高并发.低延迟.高可用.高扩展等要求在传统的关系型数据库中已经得不到满足,或者说关系型数据库应对这些需求已经显得力不从心了.关系型数据库 ...
- (十五)c#Winform自定义控件-键盘(二)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- jenkins增量更新及重启服务步骤
jenkins增量更新步骤:(以creditsys_service_tomcat为例) 1.SecureCRT 或者Xshell 连接服务器192.168.*.*,账号:test/**** 2.cd ...
- (转)2019年给Java编程初学者的建议(附学习大纲)
本文链接:https://blog.csdn.net/javajlb/article/details/85920904 1. 引言这是一篇初学者干货,请耐心看完,希望对你有帮助 作为初学者的你,命中了 ...
- springmvc集成swaggerui
这里先写下需要的pom.xml配置(我引用的2.4.0,相对稳定) 在集成springfox-swagger2之前,我也尝试着集成了swagger-springmvc,方式差不多,但是swagger- ...
- CMS和G1的区别
CMS:以获取最短回收停顿时间为目标的收集器,基于并发“标记清理”实现 过程: 1.初始标记:独占PUC,仅标记GCroots能直接关联的对象 2.并发标记:可以和用户线程并行执行,标记所有可达对象 ...
- Git安装与使用(windows环境)(一)----Git安装、生成公钥和私钥、添加SSH
安装 1.从官网下载git:http://git-scm.com/downloads 2.安装git,选择git组件安装,如下图 3.一直next,直到出现下面的窗口.这里是选择命令行形式.(可以理解 ...
- Demo小细节-2
今天在牛客的题海中再次找虐,题目如下: public class B { public static B t1 = new B(); public static B t2 = new B(); { S ...
- Markdown表格宽度调整
Markdown 表格默认宽度是根据内容来的,如果某一列内容很长的话会将其他列的宽度占用导致显示样式很丑.我们可以在表格前增加 CSS 样式来限制列的宽度: <style> table t ...
- Jenkins教程——从安装到部署Docker服务(二)声明式流水线HelloWorld
前言 本文通过一个声明式流水线的HelloWorld程序做一下流水线基础入门,对常用的流水线参数进行简要说明 什么是流水线 现实中的流水线 流水线比较好理解,类比于现实生活中的生产流水线,每个流程只做 ...