1 using System.Collections;
2 using System.Collections.Generic;
8 namespace AVR.UI.Link {
10 [RequireComponent(typeof(Dropdown))]
13 public override List<System.Type> validTypes =>
new List<System.Type> {typeof(System.Enum)};
17 protected virtual UnityEngine.Events.UnityAction<
int> updateValueListener => delegate { this.updateValue(); };
24 System.Type field_type = null;
25 if(memberType==
MemberType.FIELD) field_type = target.GetType().GetField(field).FieldType;
26 else field_type = target.GetType().GetProperty(field).PropertyType;
28 object current_value = target.GetType().GetField(field).GetValue(target);
29 string current_name = System.Enum.GetName(field_type, current_value);
31 string[] names = System.Enum.GetNames(field_type);
33 input.options.Clear();
34 for (
int i = 0; i < names.Length; i++)
36 input.options.Add(
new Dropdown.OptionData(names[i]));
37 if (names[i] == current_name) input.value = i;
44 UnityEditor.Events.UnityEventTools.RemovePersistentListener(input.onValueChanged, updateValueListener);
45 UnityEditor.Events.UnityEventTools.AddPersistentListener(input.onValueChanged, updateValueListener);
50 public override void init() {
51 if(!input) input = GetComponent<Dropdown>();
53 if(input) old_value = input.value.ToString();
59 string text = input.options[input.value].text;
65 case MemberType.FIELD:
67 int numval = (int)System.Enum.Parse(target.GetType().GetField(field).FieldType, text);
68 target.GetType().GetField(field).SetValue(target, numval);
71 case MemberType.PROPERTY:
73 int numval = (int)System.Enum.Parse(target.GetType().GetProperty(field).PropertyType, text);
74 target.GetType().GetProperty(field).SetValue(target, numval);
79 catch(System.Exception) {
80 AVR_DevConsole.cwarn(
"Could not update the given target value to "+text,
this);
89 int current_value = 0;
93 case MemberType.FIELD:
95 current_value = (int)target.GetType().GetField(field).GetValue(target);
98 case MemberType.PROPERTY:
100 current_value = (int)target.GetType().GetProperty(field).GetValue(target);
105 input.value = current_value;
108 if (old_value != current_value.ToString() && !Application.isPlaying)
110 EditorUtility.SetDirty(input);
111 old_value = current_value.ToString();
override void updateValue()
This method is executed when the runtime user changes the value of the UI element. This method should change the field on the actual target respectively.
override void setup()
Method is run when the user selects a field in the inspector.
Binds a given UI element (such as slider, toggle, dropdown or inputfield) with a given property or fi...
override void init()
Method is run on program start
override void updateOutput()
Method is run continously from OnGUI. This method should update the value displayed by the UI when th...