5using System.Collections;
6using System.Collections.Generic;
7using System.Reflection;
15 public static class pb_InspectorResolver
19 public const string TYPE_INSPECTOR_PATH =
"Required/GUI/TypeInspectorWebGL";
21 public const string TYPE_INSPECTOR_PATH =
"Required/GUI/TypeInspector";
26 private static Dictionary<Type, GameObject> inspectorPool =
new Dictionary<Type, GameObject>();
29 private static Dictionary<pb_TypeInspectorAttribute, GameObject> inspectorLookup =
null;
31 private static void InitializeLookup()
33 inspectorPool =
new Dictionary<Type, GameObject>();
34 inspectorLookup =
new Dictionary<pb_TypeInspectorAttribute, GameObject>();
36 foreach (GameObject go
in Resources.LoadAll(TYPE_INSPECTOR_PATH, typeof(GameObject)))
38 pb_TypeInspector typeInspector = go.GetComponent<pb_TypeInspector>();
40 if (typeInspector ==
null)
43 pb_TypeInspectorAttribute t = AttributeExtension.GetAttributeValue(typeInspector);
44 Attribute[] typeAttribs =
new Attribute[1] { t };
45 inspectorLookup.Add(t, go);
57 public static pb_TypeInspector GetInspector(Type type)
59 if (inspectorLookup ==
null)
62 GameObject inspectorObject;
64 if (inspectorPool.TryGetValue(type, out inspectorObject))
65 return GameObject.Instantiate(inspectorObject).GetComponent<pb_TypeInspector>();
67 List<GameObject> inspectors =
new List<GameObject>();
69 foreach (KeyValuePair<pb_TypeInspectorAttribute, GameObject> kvp
in inspectorLookup)
71 pb_TypeInspectorAttribute attrib = kvp.Key;
73 if (attrib.CanEditType(type))
75 if (attrib.type == type)
77 inspectors.Insert(0, kvp.Value);
78 goto EXACT_TYPE_INSPECTOR_FOUND;
82 inspectors.Add(kvp.Value);
88 EXACT_TYPE_INSPECTOR_FOUND:
90 if (inspectors.Count > 0)
92 inspectorPool.Add(type, inspectors[0]);
93 inspectorObject = GameObject.Instantiate(inspectors[0]);
94 pb_TypeInspector typeInspector = inspectorObject.GetComponent<pb_TypeInspector>();
95 typeInspector.SetDeclaringType(type);
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;
111 public static pb_TypeInspector AddTypeInspector(
object target, Transform parentTransform, PropertyInfo property =
null, FieldInfo field =
null)
113 pb_TypeInspector inspector =
null;
114 System.Type type =
property !=
null ?
property.PropertyType : field.FieldType;
116 inspector = pb_InspectorResolver.GetInspector(type);
118 if (inspector !=
null)
120 if (property !=
null)
121 inspector.Initialize(target, property);
123 inspector.Initialize(target, field);
125 inspector.transform.SetParent(parentTransform);
129 Debug.LogError(
"No inspector found! Is `pb_ObjectInspector.cs` missing?");
135 public static class AttributeExtension
137 public static pb_TypeInspectorAttribute GetAttributeValue(pb_TypeInspector ti)
139 var att = ti.GetType().GetCustomAttribute(typeof (pb_TypeInspectorAttribute),
true) as pb_TypeInspectorAttribute;
140 if (att !=
null && att is pb_TypeInspectorAttribute)