RouteSettingsPropertyDrawer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace ARLocation.MapboxRoutes
  4. {
  5. [CustomPropertyDrawer(typeof(MapboxRoute.RouteSettings))]
  6. public class RouteSettingsPropertyDrawer : PropertyDrawer
  7. {
  8. private SerializedProperty type;
  9. private SerializedProperty from;
  10. private SerializedProperty to;
  11. private SerializedProperty customRoute;
  12. public void FindSerializedProperties(SerializedProperty property)
  13. {
  14. type = property.FindPropertyRelative("RouteType");
  15. from = property.FindPropertyRelative("From");
  16. to = property.FindPropertyRelative("To");
  17. customRoute = property.FindPropertyRelative("CustomRoute");
  18. Debug.Assert(type != null);
  19. Debug.Assert(from != null);
  20. Debug.Assert(to != null);
  21. Debug.Assert(customRoute != null);
  22. }
  23. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  24. {
  25. FindSerializedProperties(property);
  26. var lineHeight = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  27. var height = lineHeight;
  28. // if (!property.isExpanded)
  29. // {
  30. // return height;
  31. // }
  32. // height += lineHeight;
  33. switch (type.enumValueIndex)
  34. {
  35. case (int)MapboxRoute.RouteType.CustomRoute:
  36. height += EditorGUI.GetPropertyHeight(customRoute);
  37. break;
  38. case (int)MapboxRoute.RouteType.Mapbox:
  39. height += EditorGUI.GetPropertyHeight(from);
  40. height += EditorGUI.GetPropertyHeight(to);
  41. break;
  42. }
  43. return height;
  44. }
  45. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  46. {
  47. FindSerializedProperties(property);
  48. EditorGUI.BeginProperty(position, label, property);
  49. var increment = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  50. position.height = EditorGUIUtility.singleLineHeight;
  51. var indentRect = EditorGUI.IndentedRect(position);
  52. // property.isExpanded = EditorGUI.BeginFoldoutHeaderGroup(indentRect, property.isExpanded, label);
  53. // if (property.isExpanded)
  54. // {
  55. // EditorGUI.indentLevel++;
  56. // position.y += increment;
  57. EditorGUI.PropertyField(position, type);
  58. // EditorGUI.EndFoldoutHeaderGroup();
  59. switch (type.enumValueIndex)
  60. {
  61. case (int)MapboxRoute.RouteType.Mapbox:
  62. position.y += increment;
  63. EditorGUI.PropertyField(position, from);
  64. position.y += EditorGUI.GetPropertyHeight(from);
  65. EditorGUI.PropertyField(position, to);
  66. break;
  67. case (int)MapboxRoute.RouteType.CustomRoute:
  68. position.y += increment;
  69. EditorGUI.PropertyField(position, customRoute);
  70. break;
  71. }
  72. EditorGUI.indentLevel--;
  73. // }
  74. EditorGUI.EndProperty();
  75. }
  76. }
  77. }