3using System.Collections;
4using System.Collections.Generic;
5using System.Reflection;
18 public abstract class pb_TypeInspector : MonoBehaviour
21 public string memberName =
null;
24 protected object target =
null;
27 protected PropertyInfo propertyInfo =
null;
30 protected FieldInfo fieldInfo =
null;
34 private bool ignoreSetValue =
false;
37 private int index = -1;
40 public System.Type declaringType {
get;
private set; }
45 internal void SetDeclaringType(System.Type type) { declaringType = type; }
49 public pb_TypeInspector parent;
54 public void Initialize(
object target, PropertyInfo prop)
56 Initialize(
null, target, prop);
63 public void Initialize(
string name,
object target, PropertyInfo prop)
65 if(!
string.IsNullOrEmpty(name))
66 this.memberName = name;
69 this.propertyInfo = prop;
70 this.fieldInfo =
null;
71 this.declaringType = propertyInfo.PropertyType;
73 Initialize_INTERNAL();
79 public void Initialize(
object target, FieldInfo field)
81 Initialize(
null, target, field);
87 public void Initialize(
string name,
object target, FieldInfo field)
89 if(!
string.IsNullOrEmpty(name))
90 this.memberName = name;
93 this.propertyInfo =
null;
94 this.fieldInfo = field;
95 this.declaringType = fieldInfo.FieldType;
97 Initialize_INTERNAL();
103 public void Initialize(
string name, UpdateValue getStoredValueDelegate, Callback<object> onValueChangedDelegate)
105 Initialize(name, getStoredValueDelegate, onValueChangedDelegate,
null,
null);
111 public void Initialize(
string name, UpdateValueWithIndex getStoredValueDelegate, Callback<int, object> onValueChangedDelegate)
113 Initialize(name,
null,
null, getStoredValueDelegate, onValueChangedDelegate);
120 public void Initialize(
string name,UpdateValue getStoredValueDelegate, Callback<object> onValueChangedDelegate, UpdateValueWithIndex getStoredValueDelegateIndexed, Callback<int, object> onValueChangedDelegateIndexed )
122 if(!
string.IsNullOrEmpty(name))
123 this.memberName = name;
125 this.updateValue = getStoredValueDelegate;
126 this.onValueChanged = onValueChangedDelegate;
127 this.updateValueWithIndex = getStoredValueDelegateIndexed;
128 this.onValueChangedAtIndex = onValueChangedDelegateIndexed;
130 if(declaringType ==
null)
132 object o = updateValue !=
null ? updateValue() : (updateValueWithIndex != null ? updateValueWithIndex(index) : null);
135 declaringType = o.GetType();
137 declaringType =
null;
142 Initialize_INTERNAL();
145 private void Initialize_INTERNAL()
147 gameObject.name = GetName();
150 if (useDefaultSkin) {
159 public void SetIndexInCollection(
int index)
165 public const int InputField_MinHeight = 30;
167 public static readonly
Color InputField_BackgroundColor =
new Color(1f, 1f, 1f, .8f);
168 public static readonly
Color InputField_TextColor =
new Color(0f, 38f / 255f, 137f / 255f);
170 public static readonly
Color InputField_BackgroundColor =
new Color(.32f, .32f, .32f, .8f);
171 public static readonly
Color InputField_TextColor =
Color.white;
173 virtual public bool useDefaultSkin {
get {
return true; } }
178 public void ApplyDefaultSkin()
180 foreach(InputField inputField
in gameObject.GetComponentsInChildren<InputField>())
182 LayoutElement layoutElement = inputField.gameObject.DemandComponent<LayoutElement>();
183 layoutElement.minHeight = 30f;
184 inputField.textComponent.color = InputField_TextColor;
187 foreach(
Button button
in gameObject.GetComponentsInChildren<
Button>())
189 LayoutElement layoutElement = button.gameObject.DemandComponent<LayoutElement>();
190 layoutElement.minHeight = 30f;
191 button.GetComponentInChildren<Text>().color = InputField_TextColor;
197 public delegate
object UpdateValueWithIndex(
int index);
200 public delegate
object UpdateValue();
204 public UpdateValue updateValue;
212 public UpdateValueWithIndex updateValueWithIndex;
215 public Callback onValueBeginChange =
null;
218 public Callback<int> onValueBeginChangeAtIndex =
null;
222 public Callback<object> onValueChanged =
null;
225 public Callback<int, object> onValueChangedAtIndex =
null;
230 public virtual void InitializeGUI() {
231 Debug.Log(
"Init GUI");
237 public void UpdateGUI()
239 ignoreSetValue =
true;
241 ignoreSetValue =
false;
251 protected abstract void OnUpdateGUI();
257 protected void SetValue(
object value)
263 if(++onValueSetCount == 1 && GetValue<object>() !=
null && !GetValue<object>().Equals(value))
266 if( onValueBeginChange !=
null )
267 onValueBeginChange();
269 if(onValueBeginChangeAtIndex !=
null)
270 onValueBeginChangeAtIndex(index);
272 if(propertyInfo !=
null)
275 if(fieldInfo !=
null)
279 if(onValueChanged !=
null)
281 onValueChanged(value);
284 if(onValueChangedAtIndex !=
null)
286 onValueChangedAtIndex(index, value);
289 if(propertyInfo !=
null)
294 target = System.Activator.CreateInstance(propertyInfo.PropertyType);
296 propertyInfo.SetValue(target, value,
null);
298 catch (System.Exception e)
300 Debug.LogError(e.ToString());
304 if(fieldInfo !=
null && target !=
null)
309 target = System.Activator.CreateInstance(fieldInfo.FieldType);
311 fieldInfo.SetValue(target, value);
313 catch(System.Exception e)
315 Debug.LogError(e.ToString());
319 OnInspectedValueSet();
325 protected virtual void Update()
328 if( Input.GetMouseButtonUp(0) ||
329 Input.GetMouseButtonUp(1) ||
330 Input.GetMouseButtonUp(2) ||
331 Input.GetKeyUp(KeyCode.Tab) ||
332 Input.GetKeyUp(KeyCode.Return))
341 public Callback onTypeInspectorSetValue =
null;
344 private int onValueSetCount = 0;
351 protected virtual void OnInspectedValueSet()
353 if( onTypeInspectorSetValue !=
null )
354 onTypeInspectorSetValue();
358 parent.OnInspectedValueSet();
362 if(propertyInfo !=
null)
364 else if(fieldInfo !=
null)
373 public T GetValue<T>(
object tNull =
null)
376 if (updateValue !=
null)
377 return (T)updateValue();
379 if (updateValueWithIndex !=
null)
380 return (T)updateValueWithIndex(index);
382 if(propertyInfo !=
null && target !=
null)
383 return (T) propertyInfo.GetValue(target,
null);
385 if(fieldInfo !=
null && target !=
null)
386 return (T) fieldInfo.GetValue(target);
394 public virtual string GetName()
396 if( !
string.IsNullOrEmpty(memberName) )
399 if(propertyInfo !=
null)
400 return propertyInfo.Name;
401 else if(fieldInfo !=
null)
402 return fieldInfo.Name;
404 return "Generic Type Inspector";
UnityEngine.Component Component
UnityEngine.UI.Button Button
static void AddDiff(Component component, string name, object value)
static void RegisterState(IUndo target, string message)