DropdownDrawer.cs 830 B

12345678910111213141516171819202122
  1. using UnityEditor;
  2. using UnityEngine;
  3. [CustomPropertyDrawer(typeof(DropdownAttribute))]
  4. public class DropdownDrawer : PropertyDrawer
  5. {
  6. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  7. {
  8. DropdownAttribute dropdownAttribute = attribute as DropdownAttribute;
  9. if (property.propertyType == SerializedPropertyType.String)
  10. {
  11. int selectedIndex = Mathf.Max(0, System.Array.IndexOf(dropdownAttribute.options, property.stringValue));
  12. selectedIndex = EditorGUI.Popup(position, label.text, selectedIndex, dropdownAttribute.options);
  13. property.stringValue = dropdownAttribute.options[selectedIndex];
  14. }
  15. else
  16. {
  17. EditorGUI.LabelField(position, label.text, "Use Dropdown with string.");
  18. }
  19. }
  20. }