AnimatedParameterExtensions.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. CurveEditUtility.AddKeyFrameToCurve(curve, time, curvesOwner.curves.frameRate, value, isStepped);
  51. AnimationUtility.SetEditorCurve(curvesOwner.curves, binding, curve);
  52. return true;
  53. }
  54. public static void SanitizeCurvesData(this ICurvesOwner curvesOwner)
  55. {
  56. var curves = curvesOwner.curves;
  57. if (curves == null)
  58. return;
  59. // Remove any 0-length curves
  60. foreach (var binding in AnimationUtility.GetCurveBindings(curves))
  61. {
  62. var curve = AnimationUtility.GetEditorCurve(curves, binding);
  63. if (curve.length == 0)
  64. AnimationUtility.SetEditorCurve(curves, binding, null);
  65. }
  66. // If no curves remain, delete the curves asset
  67. if (curves.empty)
  68. {
  69. var track = curvesOwner.targetTrack;
  70. var timeline = track != null ? track.timelineAsset : null;
  71. TimelineUndo.PushDestroyUndo(timeline, track, curves, "Delete Parameter Curves");
  72. }
  73. }
  74. public static bool AddAnimatedParameter(this ICurvesOwner curvesOwner, string parameterName)
  75. {
  76. var newBinding = new EditorCurveBinding();
  77. SerializedProperty property;
  78. if (!InternalAddParameter(curvesOwner, parameterName, ref newBinding, out property))
  79. return false;
  80. var duration = (float)curvesOwner.duration;
  81. CurveEditUtility.AddKey(curvesOwner.curves, newBinding, property, 0);
  82. CurveEditUtility.AddKey(curvesOwner.curves, newBinding, property, duration);
  83. return true;
  84. }
  85. public static bool RemoveAnimatedParameter(this ICurvesOwner curvesOwner, string parameterName)
  86. {
  87. if (!curvesOwner.IsParameterAnimated(parameterName) || curvesOwner.curves == null)
  88. return false;
  89. var binding = curvesOwner.GetCurveBinding(parameterName);
  90. AnimationUtility.SetEditorCurve(curvesOwner.curves, binding, null);
  91. return true;
  92. }
  93. // Set an animated parameter. Requires the field identifier 'position.x', but will add default curves to all fields
  94. public static bool SetAnimatedParameter(this ICurvesOwner curvesOwner, string parameterName, AnimationCurve curve)
  95. {
  96. // this will add a basic curve for all the related parameters
  97. if (!curvesOwner.IsParameterAnimated(parameterName) && !curvesOwner.AddAnimatedParameter(parameterName))
  98. return false;
  99. var binding = curvesOwner.GetCurveBinding(parameterName);
  100. AnimationUtility.SetEditorCurve(curvesOwner.curves, binding, curve);
  101. return true;
  102. }
  103. static bool InternalAddParameter([NotNull] ICurvesOwner curvesOwner, string parameterName, ref EditorCurveBinding binding, out SerializedProperty property)
  104. {
  105. property = null;
  106. if (curvesOwner.IsParameterAnimated(parameterName))
  107. return false;
  108. var serializedObject = AnimatedParameterUtility.GetSerializedPlayableAsset(curvesOwner.asset);
  109. if (serializedObject == null)
  110. return false;
  111. property = serializedObject.FindProperty(parameterName);
  112. if (property == null || !AnimatedParameterUtility.IsTypeAnimatable(property.propertyType))
  113. return false;
  114. if (curvesOwner.curves == null)
  115. curvesOwner.CreateCurves(curvesOwner.GetUniqueRecordedClipName());
  116. binding = curvesOwner.GetCurveBinding(parameterName);
  117. return true;
  118. }
  119. }
  120. }