1 using System.Collections;
2 using System.Collections.Generic;
7 namespace AVR.UI.Link {
9 [RequireComponent(typeof(TMPro.TMP_Dropdown))]
12 public override List<System.Type> validTypes =>
new List<System.Type> {typeof(System.Enum)};
14 private TMPro.TMP_Dropdown
input;
16 protected virtual UnityEngine.Events.UnityAction<
int> updateValueListener => delegate { this.updateValue(); };
23 System.Type field_type = null;
24 if(memberType==
MemberType.FIELD) field_type = target.GetType().GetField(field).FieldType;
25 else field_type = target.GetType().GetProperty(field).PropertyType;
27 object current_value = target.GetType().GetField(field).GetValue(target);
28 string current_name = System.Enum.GetName(field_type, current_value);
30 string[] names = System.Enum.GetNames(field_type);
32 input.options.Clear();
33 for (
int i = 0; i < names.Length; i++)
35 input.options.Add(
new TMPro.TMP_Dropdown.OptionData(names[i]));
36 if (names[i] == current_name) input.value = i;
43 UnityEditor.Events.UnityEventTools.RemovePersistentListener(input.onValueChanged, updateValueListener);
44 UnityEditor.Events.UnityEventTools.AddPersistentListener(input.onValueChanged, updateValueListener);
49 public override void init() {
50 if(!input) input = GetComponent<TMPro.TMP_Dropdown>();
52 if (input) old_value = input.value.ToString();
58 string text = input.options[input.value].text;
64 case MemberType.FIELD:
66 int numval = (int)System.Enum.Parse(target.GetType().GetField(field).FieldType, text);
67 target.GetType().GetField(field).SetValue(target, numval);
70 case MemberType.PROPERTY:
72 int numval = (int)System.Enum.Parse(target.GetType().GetProperty(field).PropertyType, text);
73 target.GetType().GetProperty(field).SetValue(target, numval);
78 catch(System.Exception) {
79 AVR_DevConsole.cwarn(
"Could not update the given target value to "+text,
this);
88 int current_value = 0;
92 case MemberType.FIELD:
94 current_value = (int)target.GetType().GetField(field).GetValue(target);
97 case MemberType.PROPERTY:
99 current_value = (int)target.GetType().GetProperty(field).GetValue(target);
104 input.value = current_value;
107 if (old_value != current_value.ToString() && !Application.isPlaying)
109 EditorUtility.SetDirty(input);
110 old_value = current_value.ToString();
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...
override void setup()
Method is run when the user selects a field in the inspector.
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.