AnimatedParameterExtensions.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Collections.Generic;
  2. using JetBrains.Annotations;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. static class AnimatedParameterExtensions
  8. {
  9. public static bool HasAnyAnimatableParameters(this ICurvesOwner curvesOwner)
  10. {
  11. return AnimatedParameterUtility.HasAnyAnimatableParameters(curvesOwner.asset);
  12. }
  13. public static IEnumerable<SerializedProperty> GetAllAnimatableParameters(this ICurvesOwner curvesOwner)
  14. {
  15. return AnimatedParameterUtility.GetAllAnimatableParameters(curvesOwner.asset);
  16. }
  17. public static bool IsParameterAnimatable(this ICurvesOwner curvesOwner, string parameterName)
  18. {
  19. return AnimatedParameterUtility.IsParameterAnimatable(curvesOwner.asset, parameterName);
  20. }
  21. public static bool IsParameterAnimated(this ICurvesOwner curvesOwner, string parameterName)
  22. {
  23. return AnimatedParameterUtility.IsParameterAnimated(curvesOwner.asset, curvesOwner.curves, parameterName);
  24. }
  25. public static EditorCurveBinding GetCurveBinding(this ICurvesOwner curvesOwner, string parameterName)
  26. {
  27. return AnimatedParameterUtility.GetCurveBinding(curvesOwner.asset, parameterName);
  28. }
  29. public static string GetUniqueRecordedClipName(this ICurvesOwner curvesOwner)
  30. {
  31. return AnimationTrackRecorder.GetUniqueRecordedClipName(curvesOwner.assetOwner, curvesOwner.defaultCurvesName);
  32. }
  33. public static AnimationCurve GetAnimatedParameter(this ICurvesOwner curvesOwner, string bindingName)
  34. {
  35. return AnimatedParameterUtility.GetAnimatedParameter(curvesOwner.asset, curvesOwner.curves, bindingName);
  36. }
  37. public static bool AddAnimatedParameterValueAt(this ICurvesOwner curvesOwner, string parameterName, float value, float time)
  38. {
  39. if (!curvesOwner.IsParameterAnimatable(parameterName))
  40. return false;
  41. if (curvesOwner.curves == null)
  42. curvesOwner.CreateCurves(curvesOwner.GetUniqueRecordedClipName());
  43. var binding = curvesOwner.GetCurveBinding(parameterName);
  44. var curve = AnimationUtility.GetEditorCurve(curvesOwner.curves, binding) ?? new AnimationCurve();
  45. var serializedObject = AnimatedParameterUtility.GetSerializedPlayableAsset(curvesOwner.asset);
  46. var property = serializedObject.FindProperty(parameterName);
  47. bool isStepped = property.propertyType == SerializedPropertyType.Boolean ||
  48. property.propertyType == SerializedPropertyType.Integer ||
  49. property.propertyType == SerializedPropertyType.Enum;
  50. TimelineUndo.PushUndo(curvesOwner.curves, "Set Key");
  51. CurveEditUtility.AddKeyFrameToCurve(curve, time, curvesOwner.curves.frameRate, value, isStepped);
  52. AnimationUtility.SetEditorCurve(curvesOwner.curves, binding, curve);
  53. return true;
  54. }
  55. public static void SanitizeCurvesData(this ICurvesOwner curvesOwner)
  56. {
  57. var curves = curvesOwner.curves;
  58. if (curves == null)
  59. return;
  60. // Remove any 0-length curves
  61. foreach (var binding in AnimationUtility.GetCurveBindings(curves))
  62. {
  63. var curve = AnimationUtility.GetEditorCurve(curves, binding);
  64. if (curve.length == 0)
  65. AnimationUtility.SetEditorCurve(curves, binding, null);
  66. }
  67. // If no curves remain, delete the curves asset
  68. if (curves.empty)
  69. {
  70. var track = curvesOwner.targetTrack;
  71. var timeline = track != null ? track.timelineAsset : null;
  72. TimelineUndo.PushDestroyUndo(timeline, track, curves);
  73. }
  74. }
  75. public static bool AddAnimatedParameter(this ICurvesOwner curvesOwner, string parameterName)
  76. {
  77. var newBinding = new EditorCurveBinding();
  78. SerializedProperty property;
  79. if (!InternalAddParameter(curvesOwner, parameterName, ref newBinding, out property))
  80. return false;
  81. var duration = (float)curvesOwner.duration;
  82. CurveEditUtility.AddKey(curvesOwner.curves, newBinding, property, 0);
  83. CurveEditUtility.AddKey(curvesOwner.curves, newBinding, property, duration);
  84. return true;
  85. }
  86. public static bool RemoveAnimatedParameter(this ICurvesOwner curvesOwner, string parameterName)
  87. {
  88. if (!curvesOwner.IsParameterAnimated(parameterName) || curvesOwner.curves == null)
  89. return false;
  90. var binding = curvesOwner.GetCurveBinding(parameterName);
  91. AnimationUtility.SetEditorCurve(curvesOwner.curves, binding, null);
  92. return true;
  93. }
  94. // Set an animated parameter. Requires the field identifier 'position.x', but will add default curves to all fields
  95. public static bool SetAnimatedParameter(this ICurvesOwner curvesOwner, string parameterName, AnimationCurve curve)
  96. {
  97. // this will add a basic curve for all the related parameters
  98. if (!curvesOwner.IsParameterAnimated(parameterName) && !curvesOwner.AddAnimatedParameter(parameterName))
  99. return false;
  100. var binding = curvesOwner.GetCurveBinding(parameterName);
  101. AnimationUtility.SetEditorCurve(curvesOwner.curves, binding, curve);
  102. return true;
  103. }
  104. static bool InternalAddParameter([NotNull] ICurvesOwner curvesOwner, string parameterName, ref EditorCurveBinding binding, out SerializedProperty property)
  105. {
  106. property = null;
  107. if (curvesOwner.IsParameterAnimated(parameterName))
  108. return false;
  109. var serializedObject = AnimatedParameterUtility.GetSerializedPlayableAsset(curvesOwner.asset);
  110. if (serializedObject == null)
  111. return false;
  112. property = serializedObject.FindProperty(parameterName);
  113. if (property == null || !AnimatedParameterUtility.IsTypeAnimatable(property.propertyType))
  114. return false;
  115. if (curvesOwner.curves == null)
  116. curvesOwner.CreateCurves(curvesOwner.GetUniqueRecordedClipName());
  117. binding = curvesOwner.GetCurveBinding(parameterName);
  118. return true;
  119. }
  120. }
  121. }