-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformExtension.cs
138 lines (126 loc) · 4.85 KB
/
TransformExtension.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Cnoom.UnityTool.Extensions
{
public static class TransformExtensions
{
/// <summary>
/// 在子物体中查找物体并添加组件
/// </summary>
/// <param name="self"></param>
/// <param name="path"></param>
/// <typeparam name="TMono"></typeparam>
/// <returns></returns>
public static TMono FindAdd<TMono>(this Transform self, string path) where TMono : MonoBehaviour
{
// 在当前Transform的子物体中查找指定路径的子物体
Transform childTransform = self.Find(path);
// 如果找到了子物体,则在该子物体上添加指定类型的组件,并返回该组件
if (childTransform!= null)
{
return childTransform.gameObject.AddComponent<TMono>();
}
// 如果没有找到子物体,则返回null
return null;
}
/// <summary>
/// 在子物体中查找物体并获取组件
/// </summary>
/// <param name="self"></param>
/// <param name="path"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T FindGet<T>(this Transform self, string path)
{
// 在当前Transform的子物体中查找指定路径的子物体
Transform childTransform = self.Find(path);
// 如果找到了子物体,则返回该子物体上的指定类型的组件
if (childTransform!= null)
{
return childTransform.gameObject.GetComponent<T>();
}
// 如果没有找到子物体,则返回null
return default(T);
}
/// <summary>
/// 在子物体及其子物体中查找物体并获取组件
/// </summary>
/// <param name="self"></param>
/// <param name="path"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T FindGetInChildren<T>(this Transform self, string path)
{
// 在当前Transform的子物体中查找指定路径的子物体
Transform childTransform = self.Find(path);
// 如果找到了子物体,则返回该子物体或其任何子物体上的指定类型的组件
if (childTransform!= null)
{
return childTransform.gameObject.GetComponentInChildren<T>();
}
// 如果没有找到子物体,则返回null
return default(T);
}
#region Children
/// <summary>
/// 对当前Transform的所有子物体执行指定的操作
/// </summary>
/// <param name="self">当前Transform对象</param>
/// <param name="action">要执行的操作</param>
/// <param name="isRecursion">是否递归执行操作</param>
public static void FuncChildren(this Transform self, Action<Transform> action, bool isRecursion = false)
{
foreach (Transform child in self)
{
action.Invoke(child);
if(isRecursion)
{
child.FuncChildren(action, true);
}
}
}
/// <summary>
/// 销毁当前Transform的所有子物体,可以指定延迟时间
/// </summary>
/// <param name="self">当前Transform对象</param>
/// <param name="duration">延迟销毁的时间</param>
public static void DestroyChildren(this Transform self, float duration = 0)
{
self.FuncChildren(t => Object.Destroy(t.gameObject, duration));
}
/// <summary>
/// 立即销毁当前Transform的所有子物体
/// </summary>
/// <param name="self">当前Transform对象</param>
public static void DestroyChildrenImmediate(this Transform self)
{
self.FuncChildren(t => Object.DestroyImmediate(t.gameObject));
}
#endregion
#region Parent
/// <summary>
/// 在当前Transform的父物体中查找指定标签的物体
/// </summary>
/// <param name="current"></param>
/// <param name="tag"></param>
/// <param name="includeSelf"></param>
/// <returns></returns>
public static Transform FindParentWithTag(this Transform current, string tag,bool includeSelf = false)
{
if(includeSelf)
{
if (current.CompareTag(tag))
{
return current;
}
}
if (current.parent == null)
{
return null;
}
return current.parent.FindParentWithTag(tag,true);
}
#endregion
}
}