Tanoda
pb_InspectorResolver.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3using UnityEngine.UI;
4using System.Linq;
5using System.Collections;
6using System.Collections.Generic;
7using System.Reflection;
8
9namespace GILES.Interface
10{
15 public static class pb_InspectorResolver
16 {
17
18#if UNITY_WEBGL
19 public const string TYPE_INSPECTOR_PATH = "Required/GUI/TypeInspectorWebGL";
20#else
21 public const string TYPE_INSPECTOR_PATH = "Required/GUI/TypeInspector";
22#endif
23
26 private static Dictionary<Type, GameObject> inspectorPool = new Dictionary<Type, GameObject>();
27
29 private static Dictionary<pb_TypeInspectorAttribute, GameObject> inspectorLookup = null;
30
31 private static void InitializeLookup()
32 {
33 inspectorPool = new Dictionary<Type, GameObject>();
34 inspectorLookup = new Dictionary<pb_TypeInspectorAttribute, GameObject>();
35
36 foreach (GameObject go in Resources.LoadAll(TYPE_INSPECTOR_PATH, typeof(GameObject)))
37 {
38 pb_TypeInspector typeInspector = go.GetComponent<pb_TypeInspector>();
39
40 if (typeInspector == null)
41 continue;
42
43 pb_TypeInspectorAttribute t = AttributeExtension.GetAttributeValue(typeInspector);
44 Attribute[] typeAttribs = new Attribute[1] { t };
45 inspectorLookup.Add(t, go);
46 }
47 }
48
49
57 public static pb_TypeInspector GetInspector(Type type)
58 {
59 if (inspectorLookup == null)
60 InitializeLookup();
61
62 GameObject inspectorObject;
63
64 if (inspectorPool.TryGetValue(type, out inspectorObject))
65 return GameObject.Instantiate(inspectorObject).GetComponent<pb_TypeInspector>();
66
67 List<GameObject> inspectors = new List<GameObject>();
68
69 foreach (KeyValuePair<pb_TypeInspectorAttribute, GameObject> kvp in inspectorLookup)
70 {
71 pb_TypeInspectorAttribute attrib = kvp.Key;
72
73 if (attrib.CanEditType(type))
74 {
75 if (attrib.type == type)
76 {
77 inspectors.Insert(0, kvp.Value);
78 goto EXACT_TYPE_INSPECTOR_FOUND;
79 }
80 else
81 {
82 inspectors.Add(kvp.Value);
83 }
84 }
85
86 }
87
88 EXACT_TYPE_INSPECTOR_FOUND:
89
90 if (inspectors.Count > 0)
91 {
92 inspectorPool.Add(type, inspectors[0]);
93 inspectorObject = GameObject.Instantiate(inspectors[0]);
94 pb_TypeInspector typeInspector = inspectorObject.GetComponent<pb_TypeInspector>();
95 typeInspector.SetDeclaringType(type);
96 return typeInspector;
97 }
98 else
99 {
100 GameObject go = new GameObject();
101 go.name = "Generic Object Inspector: " + type;
102 pb_TypeInspector typeInspector = go.AddComponent<pb_ObjectInspector>();
103 typeInspector.SetDeclaringType(type);
104 return typeInspector;
105 }
106 }
107
111 public static pb_TypeInspector AddTypeInspector(object target, Transform parentTransform, PropertyInfo property = null, FieldInfo field = null)
112 {
113 pb_TypeInspector inspector = null;
114 System.Type type = property != null ? property.PropertyType : field.FieldType;
115
116 inspector = pb_InspectorResolver.GetInspector(type);
117
118 if (inspector != null)
119 {
120 if (property != null)
121 inspector.Initialize(target, property);
122 else
123 inspector.Initialize(target, field);
124
125 inspector.transform.SetParent(parentTransform);
126 }
127 else
128 {
129 Debug.LogError("No inspector found! Is `pb_ObjectInspector.cs` missing?");
130 }
131
132 return inspector;
133 }
134 }
135 public static class AttributeExtension
136 {
137 public static pb_TypeInspectorAttribute GetAttributeValue(pb_TypeInspector ti)
138 {
139 var att = ti.GetType().GetCustomAttribute(typeof (pb_TypeInspectorAttribute), true) as pb_TypeInspectorAttribute;
140 if (att != null && att is pb_TypeInspectorAttribute)
141 {
142 return att;
143 }
144 return null;
145 }
146 }
147}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19