-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericUtils.cs
169 lines (153 loc) · 5.42 KB
/
GenericUtils.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public static class GenericUtils
{
private static List<InputField> inputFields;
#if USE_TEXT_MESH_PRO
private static List<TMP_InputField> textMeshInputFields;
#endif
private static bool isSetOnActiveSceneChanged_ResetInputField;
public static bool IsFocusInputField()
{
GameObject[] rootObjects;
#if USE_TEXT_MESH_PRO
if (inputFields == null || textMeshInputFields == null)
{
inputFields = new List<InputField>();
textMeshInputFields = new List<TMP_InputField>();
rootObjects = SceneManager.GetActiveScene().GetRootGameObjects();
foreach (GameObject rootObject in rootObjects)
{
inputFields.AddRange(rootObject.GetComponentsInChildren<InputField>(true));
textMeshInputFields.AddRange(rootObject.GetComponentsInChildren<TMP_InputField>(true));
}
}
#else
if (inputFields == null)
{
inputFields = new List<InputField>();
rootObjects = SceneManager.GetActiveScene().GetRootGameObjects();
foreach (GameObject rootObject in rootObjects)
{
inputFields.AddRange(rootObject.GetComponentsInChildren<InputField>(true));
}
}
#endif
foreach (InputField inputField in inputFields)
{
if (inputField.isFocused)
return true;
}
#if USE_TEXT_MESH_PRO
foreach (TMP_InputField inputField in textMeshInputFields)
{
if (inputField.isFocused)
return true;
}
#endif
if (!isSetOnActiveSceneChanged_ResetInputField)
{
SceneManager.activeSceneChanged += OnActiveSceneChanged_ResetInputField;
isSetOnActiveSceneChanged_ResetInputField = true;
}
return false;
}
public static void OnActiveSceneChanged_ResetInputField(Scene scene1, Scene scene2)
{
inputFields = null;
#if USE_TEXT_MESH_PRO
textMeshInputFields = null;
#endif
}
public static void SetLayerRecursively(this GameObject gameObject, int layerIndex, bool includeInactive)
{
Transform[] childrenTransforms = gameObject.GetComponentsInChildren<Transform>(includeInactive);
foreach (Transform childTransform in childrenTransforms)
{
childTransform.gameObject.layer = layerIndex;
}
}
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
{
T result = gameObject.GetComponent<T>();
if (result == null)
result = gameObject.AddComponent<T>();
return result;
}
public static void RemoveChildren(this Transform transform)
{
for (int i = transform.childCount - 1; i >= 0; --i)
{
Transform lastChild = transform.GetChild(i);
Object.Destroy(lastChild.gameObject);
}
}
public static void SetChildrenActive(this Transform transform, bool isActive)
{
for (int i = 0; i < transform.childCount; ++i)
{
transform.GetChild(i).gameObject.SetActive(isActive);
}
}
public static void RemoveObjectsByComponentInChildren<T>(this GameObject gameObject, bool includeInactive) where T : Component
{
T[] components = gameObject.GetComponentsInChildren<T>(includeInactive);
foreach (T component in components)
{
Object.DestroyImmediate(component.gameObject);
}
}
public static void RemoveObjectsByComponentInParent<T>(this GameObject gameObject, bool includeInactive) where T : Component
{
T[] components = gameObject.GetComponentsInParent<T>(includeInactive);
foreach (T component in components)
{
Object.DestroyImmediate(component.gameObject);
}
}
public static void RemoveComponents<T>(this GameObject gameObject) where T : Component
{
T[] components = gameObject.GetComponents<T>();
foreach (T component in components)
{
Object.DestroyImmediate(component);
}
}
public static void RemoveComponentsInChildren<T>(this GameObject gameObject, bool includeInactive) where T : Component
{
T[] components = gameObject.GetComponentsInChildren<T>(includeInactive);
foreach (T component in components)
{
Object.DestroyImmediate(component);
}
}
public static void RemoveComponentsInParent<T>(this GameObject gameObject, bool includeInactive) where T : Component
{
T[] components = gameObject.GetComponentsInParent<T>(includeInactive);
foreach (T component in components)
{
Object.DestroyImmediate(component);
}
}
public static int GetNegativePositive()
{
return Random.value > 0.5f ? 1 : -1;
}
public static void SetAndStretchToParentSize(this RectTransform rect, RectTransform parentRect)
{
rect.SetParent(parentRect);
rect.anchorMin = new Vector2(0, 0);
rect.anchorMax = new Vector2(1, 1);
rect.pivot = new Vector2(0.5f, 0.5f);
rect.sizeDelta = parentRect.rect.size;
rect.anchoredPosition = Vector2.zero;
rect.localScale = Vector2.one;
}
public static Color SetAlpha(this Color color, float alpha)
{
color.a = alpha;
return color;
}
}