首页 技术 正文
技术 2022年11月9日
0 收藏 495 点赞 2,616 浏览 3088 个字

导引:

因为项目中难免要多次进行获取子对象或者子对象的集合,所以写一个单独的类,用来做这些操作。然后再实际的项目中,只需要使用 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类型函数

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294