-
Notifications
You must be signed in to change notification settings - Fork 2
/
FindHelper.cs
97 lines (90 loc) · 2.75 KB
/
FindHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Tools
{
/// <summary>
/// 通过递归调用,实现在root中递归查找名字等于name的子物体
/// </summary>
/// <param name="root"></param>
/// <param name="name"></param>
/// <returns></returns>
public static Transform FindRecursively(this Transform root, string name)
{
if (root.name == name) { return root; }
foreach (Transform child in root)
{
Transform t = FindRecursively(child, name);
if (t != null)
{
return t;
}
}
return null;
}
public static T FindRecursively<T>(this Transform root, string name) where T : MonoBehaviour
{
Transform t = root.transform.FindRecursively(name);
if (t == null)
{
//Debug.LogError(string.Format("root{0}下没有子物体{1}", root.name, name));
return null;
}
T t2 = t.GetComponent<T>();
if (t2 == null)
{
//Debug.LogError(string.Format("root{0}下子物体{1}没有组件", root.name, name));
}
else
{
return t2;
}
return null;
}
/// <summary>
/// 高效查找子物体(递归查找),建议采用此种方式
/// </summary>
/// <param name="trans">父物体</param>
/// <param name="goName">子物体的名称</param>
/// <returns>找到的相应子物体</returns>
public static Transform FindChild(Transform trans, string goName)
{
Transform child = trans.Find(goName);
if (child != null)
return child;
Transform go = null;
for (int i = 0; i < trans.childCount; i++)
{
child = trans.GetChild(i);
go = FindChild(child, goName);
if (go != null)
return go;
}
return null;
}
/// <summary>
/// 查找子物体组件(递归查找子物体组件) where T : UnityEngine.Object
/// </summary>
/// <param name="trans">父物体</param>
/// <param name="goName">子物体的名称</param>
/// <returns>找到的相应子物体</returns>
public static T FindChild<T>(Transform trans, string goName) where T : Object
{
Transform child = trans.Find(goName);
if (child != null)
{
return child.GetComponent<T>();
}
Transform go = null;
for (int i = 0; i < trans.childCount; i++)
{
child = trans.GetChild(i);
go = FindChild(child, goName);
if (go != null)
{
return go.GetComponent<T>();
}
}
return null;
}
}