Tanoda
pb_GizmoManager.cs
Go to the documentation of this file.
1using UnityEngine;
2using System;
3using System.Collections;
4using System.Collections.Generic;
5using System.Linq;
6
7namespace GILES
8{
12 [System.Serializable]
13 public class pb_GizmoManager : pb_MonoBehaviourSingleton<pb_GizmoManager>
14 {
15 readonly static System.Type[] BuiltinGizmos = new System.Type[]
16 {
17 typeof(pb_Gizmo_Light),
18 typeof(pb_Gizmo_Camera)
19 };
20
21 private static Dictionary<Type, Type> gizmoLookup = null;
22
23 private static void RebuildGizmoLookup()
24 {
25 gizmoLookup = new Dictionary<Type, Type>();
26
27 foreach(Type t in BuiltinGizmos)
28 {
29 try
30 {
31 pb_GizmoAttribute attrib = (pb_GizmoAttribute) ((IEnumerable<Attribute>) t.GetCustomAttributes(true)).FirstOrDefault(x => x is pb_GizmoAttribute);
32
33 if(attrib != null)
34 gizmoLookup.Add(attrib.type, t);
35 }
36 catch (Exception e)
37 {
38
39 }
40 }
41 }
42
47 protected override void Awake()
48 {
49 base.Awake();
50
51 pb_Scene.AddOnObjectInstantiatedListener( OnObjectInstantiated );
52 pb_Scene.AddOnLevelLoadedListener( OnLevelLoaded );
53 pb_Selection.AddOnSelectionChangeListener( OnSelectionChange );
54 pb_Selection.AddOnRemovedFromSelectionListener( OnRemovedFromSelection );
55 }
56
61 void OnObjectInstantiated(GameObject go)
62 {
63 AssociateGizmos(go);
64 }
65
66 void OnSelectionChange(IEnumerable<GameObject> selection)
67 {
68 SetIsSelected(selection, true);
69 }
70
71 void OnRemovedFromSelection(IEnumerable<GameObject> selection)
72 {
73 SetIsSelected(selection, false);
74 }
75
76 void SetIsSelected(IEnumerable<GameObject> selection, bool isSelected)
77 {
78 if(selection != null)
79 {
80 foreach(GameObject go in selection)
81 {
82 pb_Gizmo[] gizmos = go.GetComponentsInChildren<pb_Gizmo>();
83
84 foreach(pb_Gizmo g in gizmos)
85 g.isSelected = isSelected;
86 }
87 }
88 }
89
94 void OnLevelLoaded()
95 {
96 foreach(GameObject go in pb_Scene.Children())
97 {
98 pb_Gizmo gizmo = AssociateGizmos(go);
99
100 if(gizmo != null && pb_Selection.gameObjects != null && pb_Selection.gameObjects.Contains(go))
101 {
102 gizmo.isSelected = true;
103 }
104 }
105 }
106
107 pb_Gizmo AssociateGizmos(GameObject go)
108 {
109 pb_Gizmo[] existing = go.GetComponents<pb_Gizmo>();
110
111 foreach(pb_Gizmo g in existing)
112 pb_ObjectUtility.Destroy(g);
113
114 foreach(Component component in go.GetComponentsInChildren<Component>())
115 {
116 if(component == null)
117 continue;
118
119 System.Type gizmo = FindGizmoForType(component.GetType());
120
121 if(gizmo != null)
122 {
123 return (pb_Gizmo) go.AddComponent(gizmo);
124 }
125 }
126
127 return null;
128 }
129
130 public static System.Type FindGizmoForType(Type type)
131 {
132 if(gizmoLookup == null)
133 RebuildGizmoLookup();
134
135 Type result = null;
136
137 gizmoLookup.TryGetValue(type, out result);
138
139 return result;
140 }
141 }
142}
UnityEngine.Component Component
Type type
The type for which this class is providing an editor.
override void Awake()
static System.Type FindGizmoForType(Type type)
static void AddOnLevelLoadedListener(Callback listener)
Definition: pb_Scene.cs:107
static void AddOnObjectInstantiatedListener(Callback< GameObject > listener)
Definition: pb_Scene.cs:96
static void AddOnSelectionChangeListener(Callback< IEnumerable< GameObject > > del)
Definition: pb_Selection.cs:34
static void AddOnRemovedFromSelectionListener(Callback< IEnumerable< GameObject > > del)
Definition: pb_Selection.cs:45