DropdownOptionListDrawer.cs 2.0 KB

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