DropdownOptionListDrawer.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using UnityEditorInternal;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace UnityEditor.UI
  5. {
  6. [CustomPropertyDrawer(typeof(Dropdown.OptionDataList), true)]
  7. /// <summary>
  8. /// This is a PropertyDrawer for Dropdown.OptionDataList. It is implemented using the standard Unity PropertyDrawer framework.
  9. /// </summary>
  10. class DropdownOptionListDrawer : PropertyDrawer
  11. {
  12. private ReorderableList m_ReorderableList;
  13. private void Init(SerializedProperty property)
  14. {
  15. if (m_ReorderableList != null)
  16. return;
  17. SerializedProperty array = property.FindPropertyRelative("m_Options");
  18. m_ReorderableList = new ReorderableList(property.serializedObject, array);
  19. m_ReorderableList.drawElementCallback = DrawOptionData;
  20. m_ReorderableList.drawHeaderCallback = DrawHeader;
  21. m_ReorderableList.elementHeight += 16;
  22. }
  23. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  24. {
  25. Init(property);
  26. m_ReorderableList.DoList(position);
  27. }
  28. private void DrawHeader(Rect rect)
  29. {
  30. GUI.Label(rect, "Options");
  31. }
  32. private void DrawOptionData(Rect rect, int index, bool isActive, bool isFocused)
  33. {
  34. SerializedProperty itemData = m_ReorderableList.serializedProperty.GetArrayElementAtIndex(index);
  35. SerializedProperty itemText = itemData.FindPropertyRelative("m_Text");
  36. SerializedProperty itemImage = itemData.FindPropertyRelative("m_Image");
  37. RectOffset offset = new RectOffset(0, 0, -1, -3);
  38. rect = offset.Add(rect);
  39. rect.height = EditorGUIUtility.singleLineHeight;
  40. EditorGUI.PropertyField(rect, itemText, GUIContent.none);
  41. rect.y += EditorGUIUtility.singleLineHeight;
  42. EditorGUI.PropertyField(rect, itemImage, GUIContent.none);
  43. }
  44. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  45. {
  46. Init(property);
  47. return m_ReorderableList.GetHeight();
  48. }
  49. }
  50. }