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