先上测试结果 没效率的东西在方便还是不好用
这是测试代码
bool ISO = true;
float times = 0;
void Start ()
{
Get_Component();
System.Threading.Thread th = new System.Threading.Thread(thr);
th.Start();
TestSpeed();
}
void thr()
{
while (ISO)
times += 0.001f;
}
void TestSpeed()
{
for(int i = 0; i < 100; i++)
{
// 分别测试这三种查找方式所用时间
// Text te1 = gameObject.FindDeepChild<Text>("Scroll View/Viewport/parent/Members");
// Text te2 = gameObject.FindDeepChild<Text>("Members");
Text te3 = transform.Find("Scroll View").Find("Viewport").Find("parent").Find("Members").GetComponent<Text>();
}
ISO = false;
print("完毕"+times);
}
起一个线程用以计时
然后查找一百遍 然后查找四层以下的子物体
第三种Find("xxx") 用时
第二种直接输入子物体名称查找组件 用时
第三种输入清楚各个层的父物体名称用时
发现\
如果直接输入子物体名称去查找 写起来很方便 但是非常消耗时间
用传统方式查找,时间很快 但是写起来有点麻烦,
输入层次关系查找 时间最快 写起来适中
实践出真知
下边是拓展类的代码 里边还有其他几个拓展方法 就不一一赘述了.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// UI扩展工具类,被类中所有方法均是对GameObject或者Transform的修饰
/// </summary>
public static class UIUtility
{
/// <summary>
/// 查找本游戏物体下的特定名称的子物体系统,并将其返回
/// </summary>
/// <param name="_target">要在其中进行查找的父物体</param>
/// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param>
/// <returns></returns>
public static Transform FindDeepChild(this GameObject _target, string _childName)
{
Transform resultTrs = null;
resultTrs = _target.transform.Find(_childName);
if (resultTrs == null)
{
foreach (Transform trs in _target.transform)
{
resultTrs = trs.gameObject.FindDeepChild(_childName);
if (resultTrs != null)
return resultTrs;
}
}
return resultTrs;
}
/// <summary>
/// 查找本游戏物体下的特定名称的子物体系统的特定组件,并将其返回
/// </summary>
/// <param name="_target">要在其中进行查找的父物体</param>
/// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param>
/// <returns>返回找到的符合条件的第一个自物体下的指定组件</returns>
public static T FindDeepChild<T>(this GameObject _target, string _childName) where T : Component
{
Transform resultTrs = _target.FindDeepChild(_childName);
if (resultTrs != null)
return resultTrs.gameObject.GetComponent<T>();
return (T)((object)null);
}
/// <summary>
/// 新建childObjectName命名的游戏物体并将其指定的子系统加入到本系统中
/// </summary>
/// <param name="target">要加入到的目标系统</param>
/// <param name="childObjectName">待加入的子物体名称</param>
public static Transform AddChild(this Transform target, string childObjectName)
{
GameObject go = new GameObject(childObjectName);
target.AddChild(go.transform);
return go.transform;
}
/// <summary>
/// 将child指定的子系统加入到本系统中
/// </summary>
/// <param name="target">要加入到的目标系统</param>
/// <param name="child">待加入的系统</param>
public static void AddChild(this Transform target, Transform child)
{
child.SetParent(target);
child.localScale = Vector3.one;
child.localPosition = Vector3.zero;
child.localEulerAngles = Vector3.zero;
ChangeChildLayer(child, target.gameObject.layer);
}
/// <summary>
/// 递归将本系统中的所有子系统的层级都指定为特定层级
/// </summary>
/// </summary>
/// <param name="t">要修改的目标系统</param>
/// <param name="layer">要指定的层级</param>
private static void ChangeChildLayer(Transform t, int layer)
{
t.gameObject.layer = layer;
foreach (Transform child in t)
{
child.gameObject.layer = layer;
ChangeChildLayer(child, layer);
}
}
}
如果有哪里不对的 敬请指出,天天学习 好好向上 共同进步