using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEditor.ShaderGraph.Drawing; using UnityEditor.UIElements; using UnityEngine; using UnityEngine.UIElements; namespace UnityEditor.ShaderGraph.Drawing.Inspector.PropertyDrawers { [SGPropertyDrawer(typeof(IEnumerable))] class DropdownPropertyDrawer : IPropertyDrawer { internal delegate void ValueChangedCallback(int newValue); internal VisualElement CreateGUI( ValueChangedCallback valueChangedCallback, IEnumerable fieldToDraw, string labelName, out VisualElement textArrayField, int indentLevel = 0) { var propertyRow = new PropertyRow(PropertyDrawerUtils.CreateLabel(labelName, indentLevel)); textArrayField = new PopupField(fieldToDraw.ToList(), 0); propertyRow.Add(textArrayField); var popupField = (PopupField)textArrayField; popupField.RegisterValueChangedCallback(evt => { valueChangedCallback(popupField.index); }); propertyRow.styleSheets.Add(Resources.Load("Styles/PropertyRow")); return propertyRow; } public Action inspectorUpdateDelegate { get; set; } public VisualElement DrawProperty(PropertyInfo propertyInfo, object actualObject, InspectableAttribute attribute) { return this.CreateGUI( // Use the setter from the provided property as the callback newSelectedIndex => propertyInfo.GetSetMethod(true).Invoke(actualObject, new object[] { newSelectedIndex }), (IEnumerable)propertyInfo.GetValue(actualObject), attribute.labelName, out var textArrayField); } } }