结论:
-
所有的寻找对象的函数原则上都不应该出现在Update/FixedUpdate这些频繁调用的函数里
-
这些函数往往在Start Awake OnEnable等这些初始化或者事件函数里使用。
Transform.Find (高效,可路径、可隐藏、)
GameObject.Find (低效)
GameObject.FindWithTag (少用)
GameObject.FindGameObjectsWithTag (少用)
Resources.FindObjectsOfTypeAll (低效,少用)
Object.FindObjectsOfType (Type type) (低效,少用)
GetComponentsInChildren( true)(低效,少用)
https://forum.unity.com/threads/gameobject-find-vs-gameobject-getcomponent.249741/
1.通过对象名称
(1).transform.Find (string name)
通过名字查找子对象并返回它,找不到返回null
- 注意如果参数中只有对象名称那么仅能查找所有儿子中的对象看是否有相同名称的,而不能查找再后面的后代。
- 但是如果参数中包含 ‘/’ 字符,将像路径一样穿越层次去查找相应名称的物体。 eg:
Transform aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");
- 然而很多情况下我们不知道对象层级(比如模型过于复杂或者想灵活控制),我们可以用递归的方式去查找:
private Transform FindChildInTransform(Transform parent,string child)
{
Transform childTF = parent.Find(child);
if(childTF != null)
{
return childTF;
}
for(int i=0;i<parent.childCount;i++)
{
childTF=FindChildInTransform(parent.GetChild(i),child);
}
return null;
}
(2). transform.GetChild(int index)
通过索引返回一个变换的子对象(仅能找儿子对象), 索引必须小于该变换的Transform.childCount(子物体数量,从0开始计数),上面在递归查找子物体的方法中已经用到。
(3).transform.parent
返回当前对象的父物体,也可通过这个属性修改当前对象所属哪个父物体。
(4).GameObject.Find(string name)
静态方法,直接用类去调用即可,和transform.Find (string name)的区别就是这个方法会从hierarchy面板中所有的对象去遍历查找第一个与参数相同的名称,显然这是一个及其消耗性能的事情,官方都说除非迫不得已,建议不要在每一帧中使用这个函数。
同样这个方法也可以使用路径作为参数。eg:
若无父物体参数写成:hand = GameObject.Find("/Monster/Arm/Hand");
有的话写成:hand = GameObject.Find("Monster/Arm/Hand");
2.通过tag(标签)查找:
(1).GameObject.FindWithTag(string tag);
静态方法,返回标记为tag的第一个游戏对象,如果没有找到对象则为空。
标签必须在使用之前到标签管理器里面声明。如果标签不存在或为空字符串或传递null作为标签,将抛出Unity异常。
(2).GameObject.FindGameObjectsWithTag(string tag)
静态方法,返回标记为tag的所有激活的游戏对象列表(GameObject[]
),如果没有找到则为空。
标签这种标记方法真是太好了,不仅节约了查找时间,还能让游戏对象有具体的分类标记。
3.通过对象类型/组件查找游戏对象
Object.FindObjectOfType (Type type);
返回Type类型第一个激活的加载的对象。
这个方法同样会遍历所有的游戏对象去找第一个有参数组件的物体。不推荐在每帧使用这个函数。
Object.FindObjectsOfType (Type type);
返回Type类型的所有激活的加载的物体列表( Object[]
)。
同样十分消耗性能。。。不推荐在每帧使用这个函数。
获取组件的方式:
GetComponent、GetComponentInChildren、GetComponentInParent、GetComponents、GetComponentsInChildren、GetComponentsInParent
获取到有关对象的组件即可通过相应组件.gameObject属性获得想要的对象/对象列表。不推荐在每帧使用这个函数.
细解:
1.GameObject.Find
函数原型: public static GameObject Find(string name);
说明:1.GameObject只能查找到active的物体
2.如果name指定路径,则按路径查找;否则递归查找,直到查找到第一个符合条件的GameObject或者返回null
2.transform.Find
函数原型: public Transform Find(string n);
说明:1.transform.Find用于查找子节点,它并不会递归的查找物体,也就是说它只会查找它的子节点,并不会查找子节点的子节点。
1.前置条件
Unity中常用到查找对象,非隐藏的、隐藏的,各种方法性能有高有低,使用又有各种条件限制。
在此对查找的性能和条件进行分析。开发时遇到的主要问题是查找隐藏对象。
没有完美的查找方法,只有最合适的查找方法
最后附带上测试代码
2.相关API
GameObject.Find
Transform.Find
GameObject.FindWithTag
GameObject.FindGameObjectsWithTag
Resources.FindObjectsOfTypeAll
2.1 GameObject.Find
通过名字或路径查找游戏对象。
GameObject.Find("GameObject");
GameObject.Find("GameObject/ChildGameObject);
使用规范:
1.无法查找隐藏对象
隐藏对象包括查找路径的任何一个父节点隐藏(active=false)
2.如果查找不在最上层,建议合理使用路径查找,路径查找是把双刃剑
优点1:解决查找中可能出现的重名问题。
优点2:如果有完全的路径,减少查找范围,减少查找时间。
缺点: 路径或结构调整后,容易影响到程序中的查找,需要重新定位查找路径。
3.如果路径查找中的任何一个父节点active=false,这个对象都将查找不到。
4.使用方便但效率低下
此查找相当于递归遍历查找,虽使用方便但效率堪忧,建议在Start()函数中查找对象并保存引用,切忌在Update()中动态查找。
2.2 Transform.Find
1.可以查找隐藏对象 (前提是transform所在的根节点必须可见,即active=true)
2.支持路径查找
GameObject root = GameObject.Find("root");
root.SetActive(false); // 根节点为空
// 总是查找失败
root.transform.Find("root/AnyChildObjectName");
实际开发:
实际开发中会将功能预制体放到一个可见的GameObject目录下,将这个GameObject目录作为查找根节点,下面的所有对象(隐藏、非隐藏)都可以查找到。
你可以把”map”节点放在一个active = true的MapRoot上,无论是关闭 或者 显示 代码中写起来都很方便。 假如你的map节点就是顶级节点,那么它一旦天生acive = false ,那么你将无法得到它的对象,更无法设置它的属性了。
GameObject root = GameObject.Find("MapRoot");
GameObject map = root.transform.Find("map").gameObject;
map.SetActive(true);
2.3 其他查找
GameObject.FindWithTag
GameObject.FindGameObjectsWithTag
使用极少,并无卵用
Resources.FindObjectsOfTypeAll
返回指定类型的对象列表。主要用于编辑器中,eg。检测内存泄露、批量查找的功能等
3 实际测试
目录结构如下,绿色代表现实,红色代表隐藏
void Start () {
// GameObject.Find
{
// 根节点
GameObject.Find("A11"); // true
// 父节点(受父节点影响)
GameObject.Find("A21"); // false
GameObject.Find("A22"); // true
// 孙子节点(受父节点影响)
GameObject.Find("A31"); // false
GameObject.Find("A32"); // false
GameObject.Find("A33"); // false
GameObject.Find("A34"); // true
GameObject.Find("A34"); // true 相对路径查找
GameObject.Find("/A34"); // false 绝对路径查找
GameObject.Find("/A11/A22/A34"); // true
GameObject.Find("A11/A22/A34"); // true
GameObject.Find("/A22/A34"); // false
GameObject.Find("A22/A34"); // true
}
// Transform.find
{
// 根节点
Transform A11 = transform.Find("A11"); // false
// 父亲节点
Transform A21 = transform.Find("A21"); // true
Transform A22 = transform.Find("A22"); // true
// 孙子节点
Transform A31 = transform.Find("A31"); // false
Transform A32 = transform.Find("A32"); // false
Transform A33 = transform.Find("A33"); // false
Transform A34 = transform.Find("A34"); // false
// 使用相对于根节点的查找目录
Transform AA31 = transform.Find("A21/A31"); // true
Transform AA32 = transform.Find("A21/A32"); // true
Transform AA33 = transform.Find("A22/A33"); // true
Transform AA34 = transform.Find("A22/A34"); // true
// 包含根节点的查找目录
Transform AA311 = transform.Find("A11/A21/A31"); // false
Transform AA321 = transform.Find("A11/A21/A32"); // false
Transform AA331 = transform.Find("A11/A22/A33"); // false
Transform AA341 = transform.Find("A11/A22/A34"); // false
// 绝对路径
Transform AA3111 = transform.Find("/A11/A21/A31"); // false
Transform AA3211 = transform.Find("/A11/A21/A32"); // false
Transform AA3311 = transform.Find("/A11/A22/A33"); // false
Transform AA3411 = transform.Find("/A11/A22/A34"); // false
}
}
4 即使隐藏root节点gameObject也能进行查找的方法
找到了一个即使隐藏root节点gameObject也能进行查找的方法。
http://answers.unity3d.com/questions/52560/gameobjectfind-work-on-inactive-objects.html
代码预览:
GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));
foreach (GameObject pObject in pAllObjects)
{
if (pObject.transform.parent != null)
{
continue;
}
if (pObject.hideFlags == HideFlags.NotEditable || pObject.hideFlags == HideFlags.HideAndDontSave)
{
continue;
}
if (Application.isEditor)
{
string sAssetPath = AssetDatabase.GetAssetPath(pObject.transform.root.gameObject);
if (!string.IsNullOrEmpty(sAssetPath))
{
continue;
}
}
Debug.Log(pObject.name);
}
例: 调用方法的脚本没挂在此面板节点(组件)上,
实现右键移动这个面板
// 全局静态方法
public static class Globe
{
/// <summary>
/// UI根节点
/// </summary>
public static GameObject RootCanvas;
/// <summary>
/// 普通UI节点
/// </summary>
public static GameObject NormalRoot;
/// <summary>
/// 固定UI节点
/// </summary>
public static GameObject FixedRoot;
/// <summary>
/// 弹窗UI节点
/// </summary>
public static GameObject PopUpRoot;
public static void SetRootCanvas()
{
if (RootCanvas == null)
{
// 根据标签找到根节点,再查其子节点
RootCanvas = GameObject.FindGameObjectWithTag(SysDefine.SYS_TAG_CANVAS);
NormalRoot = UnityHelper.FindTheChildNode(RootCanvas, "Normal").gameObject;
FixedRoot = UnityHelper.FindTheChildNode(RootCanvas, "Fixed").gameObject;
PopUpRoot = UnityHelper.FindTheChildNode(RootCanvas, "PopUp").gameObject;
}
}
/// <summary>
/// 只显示(激活)弹窗UI
/// </summary>
public static void OnlyShowPopUpRoot()
{
CommonTools.Tool.Log("OnlyShowPopUpRoot()");
Debug.Log("OnlyShowPopUpRoot");
if (RootCanvas != null)
{
RootCanvas.SetActive(true);
NormalRoot.SetActive(false);
FixedRoot.SetActive(false);
PopUpRoot.SetActive(true);
}
}
}
// 调用方法
public class BagUI : BaseUIForm
{
protected override void ReceiveMsg(KeyValuesUpdate KV)
{
switch (KV.Key)
{
case "MouseRightExit":
{
// 获取面板组件实例
Transform fixRootNode = Globe.FixedRoot.transform;
Transform missionTipsHide = FindChild(fixRootNode.gameObject, "HideButton");
if (missionTipsHide.gameObject.activeInHierarchy == true)
{
SendMessage("MissionTipsUI", "MissionTipsNeedHide");
}
}
break;
}
}
// 需要移动的面板
public class MissionTipsUI : BaseUIForm
{
protected override void ReceiveMsg(KeyValuesUpdate KV)
{
switch (KV.Key)
{
case "MissionTipsNeedHide":
{
StartCoroutine( MissionTipsMoveIE(false) );
}
break;
}
}
}