AreaClockEditor.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace Chronos
  4. {
  5. public abstract class AreaClockEditor<TAreaClock> : ClockEditor where TAreaClock : Component, IAreaClock
  6. {
  7. protected SerializedProperty mode;
  8. protected SerializedProperty curve;
  9. protected SerializedProperty center;
  10. protected SerializedProperty padding;
  11. protected SerializedProperty innerBlend;
  12. public override void OnEnable()
  13. {
  14. base.OnEnable();
  15. mode = serializedObject.FindProperty("_mode");
  16. curve = serializedObject.FindProperty("_curve");
  17. center = serializedObject.FindProperty("_center");
  18. padding = serializedObject.FindProperty("_padding");
  19. innerBlend = serializedObject.FindProperty("_innerBlend");
  20. }
  21. protected abstract void CheckForCollider();
  22. public override void OnInspectorGUI()
  23. {
  24. serializedObject.Update();
  25. if (!serializedObject.isEditingMultipleObjects)
  26. {
  27. TAreaClock clock = (TAreaClock)serializedObject.targetObject;
  28. Timekeeper timekeeper = clock.GetComponent<Timekeeper>();
  29. if (timekeeper != null)
  30. {
  31. EditorGUILayout.HelpBox("Only global clocks should be attached to the timekeeper.", MessageType.Error);
  32. }
  33. CheckForCollider();
  34. }
  35. base.OnInspectorGUI();
  36. EditorGUILayout.PropertyField(mode, new GUIContent("Mode"));
  37. if (!mode.hasMultipleDifferentValues &&
  38. mode.enumValueIndex != (int)AreaClockMode.Instant)
  39. {
  40. if (!serializedObject.isEditingMultipleObjects) // TODO: Multiple object editing
  41. {
  42. // Bug: Doesn't work in Unity 2017.2.
  43. // https://issuetracker.unity3d.com/issues/editorguilayout-dot-curvefield-does-not-return-edited-curve-when-dynamically-creating-curve
  44. // https://issuetracker.unity3d.com/issues/animationcurve-value-cannot-be-changed-in-custom-inspector-when-it-is-accessed-as-serialized-propertys-animationcurvevalue
  45. // curve.animationCurveValue = EditorGUILayout.CurveField(new GUIContent("Curve"),
  46. // curve.animationCurveValue,
  47. // Color.magenta,
  48. // new Rect(0, -1, 1, 2),
  49. // GUILayout.Height(30));
  50. // Moreover, CurveField doesn't seemt trigger GUI.changed, so we can't rely on that for optimization
  51. // Fallback to fix the bug:
  52. Undo.RecordObject(serializedObject.targetObject, "Modify Area Clock Curve");
  53. TAreaClock clock = (TAreaClock)serializedObject.targetObject;
  54. clock.curve = EditorGUILayout.CurveField(new GUIContent("Curve"),
  55. clock.curve,
  56. Color.magenta,
  57. new Rect(0, -1, 1, 2),
  58. GUILayout.Height(30));
  59. }
  60. if (mode.enumValueIndex == (int)AreaClockMode.PointToEdge)
  61. {
  62. EditorGUILayout.PropertyField(center, new GUIContent("Center"));
  63. }
  64. else if (mode.enumValueIndex == (int)AreaClockMode.DistanceFromEntry)
  65. {
  66. EditorGUILayout.PropertyField(padding, new GUIContent("Padding"));
  67. }
  68. }
  69. serializedObject.ApplyModifiedProperties();
  70. }
  71. protected override void OnBlendsGUI()
  72. {
  73. base.OnBlendsGUI();
  74. EditorGUILayout.PropertyField(innerBlend, new GUIContent("Inner Blend"));
  75. }
  76. }
  77. }