TimeFieldDrawer.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. [CustomPropertyDrawer(typeof(TimeFieldAttribute), true)]
  8. class TimeFieldDrawer : PropertyDrawer
  9. {
  10. static WindowState state
  11. {
  12. get { return TimelineWindow.instance != null ? TimelineWindow.instance.state : null; }
  13. }
  14. static float currentFrameRate
  15. {
  16. get { return state != null ? TimelineWindow.instance.state.referenceSequence.frameRate : 0.0f; }
  17. }
  18. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  19. {
  20. if (property.propertyType != SerializedPropertyType.Float)
  21. {
  22. GUILayout.Label("TimeField only works on floating point types");
  23. return;
  24. }
  25. var timeFieldAttribute = attribute as TimeFieldAttribute;
  26. if (timeFieldAttribute == null)
  27. return;
  28. var rect = EditorGUILayout.s_LastRect;
  29. EditorGUI.BeginChangeCheck();
  30. if (timeFieldAttribute.useEditMode == TimeFieldAttribute.UseEditMode.ApplyEditMode)
  31. TimeFieldWithEditMode(rect, property, label);
  32. else
  33. TimeField(rect, property, label);
  34. if (EditorGUI.EndChangeCheck())
  35. {
  36. if (state != null)
  37. state.Refresh();
  38. }
  39. }
  40. static void TimeField(Rect rect, SerializedProperty property, GUIContent label)
  41. {
  42. var evt1 = InputEvent.None;
  43. TimelineInspectorUtility.TimeField(rect, property, label, false, currentFrameRate, 0, float.MaxValue, ref evt1);
  44. }
  45. static void TimeFieldWithEditMode(Rect rect, SerializedProperty property, GUIContent label)
  46. {
  47. double minStartTime;
  48. if (property.hasMultipleDifferentValues)
  49. minStartTime = SelectionManager.SelectedItems().Min(i => i.start);
  50. else
  51. minStartTime = property.doubleValue;
  52. var evt = InputEvent.None;
  53. var newValue = TimelineInspectorUtility.TimeField(
  54. rect, label, minStartTime, false, property.hasMultipleDifferentValues, currentFrameRate, 0.0, float.MaxValue, ref evt);
  55. EditMode.inputHandler.ProcessMove(evt, newValue);
  56. }
  57. }
  58. }