Tanoda
pb_Hierarchy.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using UnityEngine;
4using System.Reflection;
5using UnityEngine.UI;
7using System.Linq;
8using System.Collections.Generic;
10using UnityEditor;
11
12namespace GILES.Interface
13{
17 public class pb_Hierarchy : pb_MonoBehaviourSingleton<pb_Hierarchy>
18 {
21 public GameObject inspectorScrollPanel;
22
23 private GameObject hierarchyRoot;
24
25 private List<GameObject> savedHighlights = new List<GameObject>();
26
27 //private readonly GameObjectSearcher _gameObjectSearcher = new GameObjectSearcher();
28 private readonly List<GameObject> _openedObjects = new List<GameObject>();
29
30 void Start()
31 {
32 StartCoroutine(DelayedStart());
33 }
34
35 IEnumerator DelayedStart()
36 {
37 yield return new WaitForSeconds(0.1f);
38 //_gameObjectSearcher.Refresh(true, (Predicate<GameObject>)null);
39 hierarchyRoot = GameObject.Find("Level Editor SceneGraph Root");
40 var currentCount = 0;
41 DisplayObjectTreeHelper(hierarchyRoot, 0, ref currentCount);
42 }
43
44 public void Search(string text)
45 {
46 _openedObjects.Clear();
47 ClearInspector();
48 var currentCount = 0;
49 DisplayObjectTreeHelper(hierarchyRoot, 0, ref currentCount);
50 if (text == "")
51 {
52 return;
53 }
54 var allFind = inspectorScrollPanel.GetComponentsInChildren<Transform>(true).Where(x => x.name.Contains(text));
55 //Debug.Log($"allFind: {allFind.Count()}");
56
57 //HighlightSelected(inspectorScrollPanel.transform, Array.ConvertAll(allFind.ToArray(), t => t.gameObject).ToList());
58 foreach (var item in allFind)
59 {
60 _openedObjects.Add(item.gameObject);
61
62 item.gameObject.SetActive(true);
63
64 var parents = item.GetComponentsInParent<Transform>(true);
65 foreach (var o in parents)
66 {
67 o.gameObject.SetActive(true);
68 }
69 }
70 }
71
72 internal void ToggleHierarchy(GameObject panel, GameObject selectable = null)
73 {
74 if (Input.GetKey(KeyCode.LeftControl))
75 {
76 if (savedHighlights.Contains(selectable))
77 {
79 }
80 else
81 {
82 pb_Selection.AddToSelection(selectable);
83 }
84 }
85 else
86 {
87 pb_Selection.SetSelection(selectable);
88 }
89 if (_openedObjects.Contains(panel))
90 {
91 if (!savedHighlights.Contains(selectable))
92 {
93 return;
94 }
95 _openedObjects.Remove(panel);
96 var childs = panel.GetComponentsInChildren<Transform>(true);
97 foreach (var o in childs)
98 {
99 if (o.gameObject != panel)
100 if (o.name != "Label Field")
101 o.gameObject.SetActive(false);
102 }
103 }
104 else
105 {
106 _openedObjects.Add(panel);
107 var childs = panel.GetComponentsInChildren<Transform>(true);
108 foreach (var o in childs)
109 {
110 o.gameObject.SetActive(true);
111 }
112 }
113 //RebuildInspector(hierarchyRoot);
114 }
115
116 public void HighlightSelected(GameObject selection)
117 {
118 //ClearInspector();
119 //
120 //var go = hierarchyRoot;
121 //var currentCount = 0;
122
123 savedHighlights.Add(selection);
124 //DisplayObjectTreeHelper(go, 0, ref currentCount, null, false, selection);
125
126 HighlightSelected(inspectorScrollPanel.transform, savedHighlights);
127 }
128
129 public void RemoveSelected(GameObject selection)
130 {
131 savedHighlights.Remove(selection);
132
133 HighlightSelected(inspectorScrollPanel.transform, savedHighlights);
134 }
135
136 private void HighlightSelected(Transform p, List<GameObject> selection)
137 {
138 foreach (Transform c in p)
139 {
140 if (c.GetComponent<ReferenceSaver>())
141 {
142 if (selection.Contains(c.GetComponent<ReferenceSaver>().Reference))
143 {
144 if (!c.gameObject.GetComponent<Outline>())
145 {
146 var outline = c.gameObject.AddComponent<Outline>();
147 outline.effectColor = Color.white;
148 outline.effectDistance = Vector2.one;
149 }
150 c.gameObject.SetActive(true);
151
152 var parents = c.GetComponentsInParent<Transform>(true);
153 foreach (var o in parents)
154 {
155 o.gameObject.SetActive(true);
156 }
157
158 }
159 else
160 {
161 DestroyImmediate(c.gameObject.GetComponent<Outline>());
162 }
163 }
164
165 foreach (Transform cc in c)
166 {
167 if (cc.GetComponent<ReferenceSaver>())
168 {
169 if (selection.Contains(cc.GetComponent<ReferenceSaver>().Reference))
170 {
171 if (!cc.GetComponent<Outline>())
172 {
173 var outline = cc.gameObject.AddComponent<Outline>();
174 outline.effectColor = Color.white;
175 outline.effectDistance = Vector2.one;
176 }
177 if (!cc.gameObject.activeSelf)
178 {
179 cc.gameObject.SetActive(true);
180 }
181 var parents = c.GetComponentsInParent<Transform>(true);
182 foreach (var o in parents)
183 {
184 o.gameObject.SetActive(true);
185 }
186 }
187 else
188 {
189 DestroyImmediate(cc.gameObject.GetComponent<Outline>());
190 }
191 }
192 HighlightSelected(cc, selection);
193 }
194 }
195 }
196
197 private bool DisplayObjectTreeHelper(GameObject go, int indent, ref int currentCount, GameObject parentPanel = null,
198 bool closed = false, List<GameObject> selection = null)
199 {
200 var retval = false;
201 currentCount++;
202 if (indent != 0)
203 {
204 var goRo = go.GetComponent<RenameObject>();
205 var goName = goRo ? string.IsNullOrEmpty(goRo.NewName) ? go.name.ToUpper() : goRo.NewName.ToUpper() : go.name.ToUpper();
206 GameObject panel = pb_GUIUtility.CreateLabeledVerticalPanel(goName);
207 panel.GetComponent<VerticalLayoutGroup>().padding = new RectOffset(8,4,4,4);
208 var cell = panel.AddComponent<DragAndDropCell>();
209 cell.unlimitedSource = true;
210 cell.cellType = DragAndDropCell.CellType.Swap;
211 cell.empty = Color.white;
212 cell.empty.a = 16 / 255f;
213 cell.full = Color.white;
214 cell.full.a = 16/255f;
215 cell.ignoreColor = false;
216 panel.AddComponent<DragAndDropItem>();
217 panel.AddComponent<OnDraggedEventHierarchy>();
218 panel.transform.SetParent(parentPanel ? parentPanel.transform : inspectorScrollPanel.transform);
219 panel.AddComponent<ClickAction>();
220 var button = panel.AddComponent<Button>();
221 button.transition = Selectable.Transition.None;
222 if (selection != null && selection.Contains(go))
223 {
224 if (!panel.GetComponent<Outline>())
225 {
226 var outline = panel.AddComponent<Outline>();
227 outline.effectColor = Color.white;
228 outline.effectDistance = Vector2.one;
229 }
230 }
231 var rs = panel.AddComponent<ReferenceSaver>();
232 rs.Reference = go;
233 panel.SetActive(!closed);
234 retval = true;
235 for (var i = 0; i < go.transform.childCount; ++i)
236 DisplayObjectTreeHelper(go.transform.GetChild(i).gameObject, indent + 1, ref currentCount, panel,
237 !_openedObjects.Contains(go), selection);
238 }
239 else
240 {
241 for (var i = 0; i < go.transform.childCount; ++i)
242 if (DisplayObjectTreeHelper(go.transform.GetChild(i).gameObject, indent + 1, ref currentCount, selection: selection))
243 retval = true;
244 }
245
246 return retval;
247 }
248
249 public void RebuildInspector(GameObject go)
250 {
251 ClearInspector();
252
253 if (go == null) go = hierarchyRoot;
254
255
256 var currentCount = 0;
257 DisplayObjectTreeHelper(go, 0, ref currentCount, selection: savedHighlights);
258 }
259
260 public void RebuildInspector()
261 {
262 ClearInspector();
263 StopAllCoroutines();
264 StartCoroutine(DelayedRebuild());
265 }
266
267 IEnumerator DelayedRebuild()
268 {
269 yield return new WaitForSeconds(0.1f);
270 ClearInspector();
271 yield return new WaitForEndOfFrame();
272
273 var currentCount = 0;
274 if (!DisplayObjectTreeHelper(hierarchyRoot, 0, ref currentCount))
275 {
276 yield return new WaitForSeconds(0.5f);
277 DisplayObjectTreeHelper(hierarchyRoot, 0, ref currentCount);
278 }
279 }
280
281 void ClearInspector()
282 {
284 foreach(Transform go in inspectorScrollPanel.transform)
285 pb_ObjectUtility.Destroy(go.gameObject);
286 }
287
291 public void ToggleInspector(bool show)
292 {
293 // GetComponent<RectTransform>().bottom = show ? 0f : 200f;
294 }
295 }
296 public class ClickAction : MonoBehaviour, IPointerClickHandler
297 {
298 public void OnPointerClick(PointerEventData eventData)
299 {
300 var temp = FindObjectOfType<pb_Hierarchy>();
301 var rs = gameObject.GetComponent<ReferenceSaver>();
302 temp.ToggleHierarchy(gameObject, rs.Reference);
303
304 if (eventData.clickCount == 2)
305 {
306 pb_SceneCamera.Focus(rs.Reference.transform.position, 0.5f);
307 }
308 }
309 }
310
311#if UNITY_EDITOR
312 [CustomEditor(typeof(pb_Hierarchy))]
313 public class pb_HierarchyInspector : Editor
314 {
315 public override void OnInspectorGUI ()
316 {
317 DrawDefaultInspector();
318
319 pb_Hierarchy myTarget = (pb_Hierarchy)target;
320 GUILayout.BeginHorizontal();
321 if (GUILayout.Button("Rebuild Inspector"))
322 {
323 myTarget.RebuildInspector();
324 }
325 GUILayout.EndHorizontal();
326 }
327 }
328#endif
329}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Color Color
Definition: TestScript.cs:32
Every item's cell must contain this script
Drag and Drop item.
void OnPointerClick(PointerEventData eventData)
void RebuildInspector(GameObject go)
void HighlightSelected(GameObject selection)
void RemoveSelected(GameObject selection)
void Search(string text)
Definition: pb_Hierarchy.cs:44
void ToggleInspector(bool show)
static void Focus(Vector3 target)
static void RemoveFromSelection(GameObject go)
static void AddToSelection(GameObject go)
GameObject Reference
string NewName
Definition: RenameObject.cs:7