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