RouteWaypointPropertyDrawer.cs 3.0 KB

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