AnimationCurve
How do I use Animation Curve
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PhotonTransformViewPositionModel.cs
Copy
35 public AnimationCurve InterpolateSpeedCurve = new AnimationCurve( new Keyframe[] {
36 new Keyframe( -1, 0, 0, Mathf.Infinity ),
37 new Keyframe( 0, 1, 0, 0 ),
38 new Keyframe( 1, 1, 0, 1 ),
39 new Keyframe( 4, 4, 1, 0 ) } );
File name: AnimationBuilder.cs
Copy
91 public AnimationClip MakeAnimationClip(GameObject root, Animation animation, string baseFolderPath)
92 {
93 //Clear local caches
94 lastGameObjectCache.Clear();
95 animationEvents.Clear();
96 lastKeyframeCache.Clear();
97 spriteChangeKeys.Clear();
98 spriteBaseFolder = baseFolderPath;
99
100 var animClip = new AnimationClip();
101 animClip.name = animation.Name;
102
103 //Set clip to Generic type
104 //AnimationUtility.SetAnimationType(animClip, ModelImporterAnimationType.Generic);
105
106 //Populate the animation curves & events
107 MakeAnimationCurves(root, animClip, animation);
108
109 return animClip;
110 }
File name: AnimationBuilder.cs
Copy
112 private void MakeAnimationCurves(GameObject root, AnimationClip animClip, Animation animation)
113 {
114 acb = new AnimationCurveBuilder();
115
116 //Get a list of all sprites on this GO
117 var allSprites = root.GetComponentsInChildren
118
119 //Add a key for all objects on the first frame
120 SetGameObjectForKey(root, animClip, animation.MainlineKeys.First(), 0);
121
122 foreach (var mainlineKey in animation.MainlineKeys)
123 {
124
125 var visibleSprites = SetGameObjectForKey(root, animClip, mainlineKey);
126 var hiddenSprites = allSprites.Except(visibleSprites);
127 HideSprites(root, hiddenSprites, mainlineKey.Time);
128 }
129
130 switch (animation.LoopType)
131 {
132 case LoopType.True:
133 //Cycle back to first frame
134 SetGameObjectForKey(root, animClip, animation.MainlineKeys.First(), animation.Length);
135 break;
136 case LoopType.False:
137 //Duplicate the last key at the end time of the animation
138 SetGameObjectForKey(root, animClip, animation.MainlineKeys.Last(), animation.Length);
139 break;
140 default:
141 Debug.LogWarning("Unsupported loop type: " + animation.LoopType.ToString());
142 break;
143 }
144
145 //Add the curves to our animation clip
146 //NOTE: This MUST be done before modifying the settings, thus the double switch statement
147 acb.AddCurves(animClip);
148
149 foreach (var sprite in spriteChangeKeys)
150 {
151 if (sprite.Value.Count > 0)
152 {
153 BuildSpriteChangeCurve(ref animClip, sprite);
154 }
155 }
156
157 //Set the loop/wrap settings for the animation clip
158 var animSettings = AnimationUtility.GetAnimationClipSettings(animClip);
159 switch(animation.LoopType)
160 {
161 case LoopType.True:
162 animClip.wrapMode = WrapMode.Loop;
163 animSettings.loopTime = true;
164 break;
165 case LoopType.False:
166 animClip.wrapMode = WrapMode.ClampForever;
167 break;
168 case LoopType.PingPong:
169 animClip.wrapMode = WrapMode.PingPong;
170 animSettings.loopTime = true;
171 break;
172 default:
173 Debug.LogWarning("Unsupported loop type: " + animation.LoopType.ToString());
174 break;
175 }
176
177 animClip.SetAnimationSettings(animSettings);
178
179 //Debug.Log(string.Format("Setting animation {0} to {1} loop mode (WrapMode:{2} LoopTime:{3}) ", animClip.name, animation.LoopType, animClip.wrapMode, animSettings.loopTime));
180 }
File name: AnimationCurveBuilder.cs
Copy
38 {
39 public bool IsSpriteKey;
40 public AnimationCurve[] Curves;
41 }
File name: AnimationCurveBuilder.cs
Copy
45 public void AddCurves(AnimationClip animClip)
46 {
47 foreach(var kvp in curveCache)
48 {
49 var curves = kvp.Value.Curves;
50 //Position curves
51 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localPosition.x", curves[(int)AnimationCurveIndex.LocalPositionX]);
52 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localPosition.y", curves[(int)AnimationCurveIndex.LocalPositionY]);
53 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localPosition.z", curves[(int)AnimationCurveIndex.LocalPositionZ]);
54
55 //Rotation curves
56 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localRotation.x", curves[(int)AnimationCurveIndex.LocalRotationX]);
57 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localRotation.y", curves[(int)AnimationCurveIndex.LocalRotationY]);
58 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localRotation.z", curves[(int)AnimationCurveIndex.LocalRotationZ]);
59 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localRotation.w", curves[(int)AnimationCurveIndex.LocalRotationW]);
60
61 //Scale curves
62 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localScale.x", curves[(int)AnimationCurveIndex.LocalScaleX]);
63 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localScale.y", curves[(int)AnimationCurveIndex.LocalScaleY]);
64 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(Transform), "localScale.z", curves[(int)AnimationCurveIndex.LocalScaleZ]);
65
66 //IsActive curve
67 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(GameObject), "m_IsActive", curves[(int)AnimationCurveIndex.IsActive]);
68
69 if (kvp.Value.IsSpriteKey)
70 {
71 //Color Tint curve
72 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(SpriteRenderer), "m_Color.r", curves[(int)AnimationCurveIndex.ColorR]);
73 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(SpriteRenderer), "m_Color.g", curves[(int)AnimationCurveIndex.ColorG]);
74 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(SpriteRenderer), "m_Color.b", curves[(int)AnimationCurveIndex.ColorB]);
75 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(SpriteRenderer), "m_Color.a", curves[(int)AnimationCurveIndex.ColorA]);
76 SetCurveIfNotEmpty(ref animClip, kvp.Key, typeof(SortingOrderUpdate), "SortingOrder", curves[(int)AnimationCurveIndex.ZIndex]);
77 }
78 }
79
80 //animClip.EnsureQuaternionContinuity();
81 }
File name: AnimationCurveBuilder.cs
Copy
103 public void SetCurve(Transform root, Transform current, float time, TimelineKey lastTimelineKey, int zIndex = -1)
104 {
105 var path = AnimationUtility.CalculateTransformPath(current, root);
106 var curves = GetOrCreateAnimationCurves(path);
107 UpdateTransformCurve(curves, current, time, lastTimelineKey, zIndex);
108 }
File name: AnimationCurveBuilder.cs
Copy
110 public void SetCurveActiveOnly(Transform root, Transform current, float time)
111 {
112 var path = AnimationUtility.CalculateTransformPath(current, root);
113 var obj = GetOrCreateAnimationCurves(path);
114
115 //IsActive curve
116 float val = (current.gameObject.activeInHierarchy) ? 1.0f : 0.0f;
117 obj.Curves[(int)AnimationCurveIndex.IsActive].AddKey(new Keyframe(time, val, float.PositiveInfinity, float.PositiveInfinity) { tangentMode = 0 });
118 }
File name: AnimationCurveBuilder.cs
Copy
120 private void SetCurveIfNotEmpty(ref AnimationClip clip, string path, Type component, string property, AnimationCurve curve)
121 {
122 if (curve.keys.Length > 0)
123 {
124 clip.SetCurve(path, component, property, curve);
125 }
126 }
File name: AnimationCurveBuilder.cs
Copy
128 private void UpdateTransformCurve(ObjectCurves obj, Transform current, float time, TimelineKey lastTimelineKey, int zIndex = -1)
129 {
130 float val;
131 //IsActive curve
132 val = (current.gameObject.activeSelf) ? 1.0f : 0.0f;
133 obj.Curves[(int)AnimationCurveIndex.IsActive].AddKey(new Keyframe(time, val, float.PositiveInfinity, float.PositiveInfinity) { tangentMode = 0 });
134
135 //Position curves
136 obj.Curves[(int)AnimationCurveIndex.LocalPositionX].AddKey(new Keyframe(time, current.localPosition.x) { tangentMode = 0 }, lastTimelineKey);
137 obj.Curves[(int)AnimationCurveIndex.LocalPositionY].AddKey(new Keyframe(time, current.localPosition.y) { tangentMode = 0 }, lastTimelineKey);
138 obj.Curves[(int)AnimationCurveIndex.LocalPositionZ].AddKey(new Keyframe(time, current.localPosition.z, float.PositiveInfinity, float.PositiveInfinity)); //Z value always has instant transition
139
140 //Rotation curves
141 var quat = Quaternion.Euler(current.localEulerAngles);
142 obj.Curves[(int)AnimationCurveIndex.LocalRotationX].AddKey(new Keyframe(time, quat.x) { tangentMode = 0 }, lastTimelineKey);
143 obj.Curves[(int)AnimationCurveIndex.LocalRotationY].AddKey(new Keyframe(time, quat.y) { tangentMode = 0 }, lastTimelineKey);
144 obj.Curves[(int)AnimationCurveIndex.LocalRotationZ].AddKey(new Keyframe(time, quat.z) { tangentMode = 0 }, lastTimelineKey);
145 obj.Curves[(int)AnimationCurveIndex.LocalRotationW].AddKey(new Keyframe(time, quat.w) { tangentMode = 0 }, lastTimelineKey);
146
147 //Scale curves
148 obj.Curves[(int)AnimationCurveIndex.LocalScaleX].AddKey(new Keyframe(time, current.localScale.x) { tangentMode = 0 }, lastTimelineKey);
149 obj.Curves[(int)AnimationCurveIndex.LocalScaleY].AddKey(new Keyframe(time, current.localScale.y) { tangentMode = 0 }, lastTimelineKey);
150 obj.Curves[(int)AnimationCurveIndex.LocalScaleZ].AddKey(new Keyframe(time, current.localScale.z) { tangentMode = 0 }, lastTimelineKey);
151
152 //Sprite Curves
153 var spriteTimelineKey = lastTimelineKey as SpriteTimelineKey;
154 if (spriteTimelineKey != null)
155 {
156 obj.IsSpriteKey = true;
157 obj.Curves[(int)AnimationCurveIndex.ColorR].AddKey(new Keyframe(time, spriteTimelineKey.Tint.r) { tangentMode = 0 }, lastTimelineKey);
158 obj.Curves[(int)AnimationCurveIndex.ColorG].AddKey(new Keyframe(time, spriteTimelineKey.Tint.g) { tangentMode = 0 }, lastTimelineKey);
159 obj.Curves[(int)AnimationCurveIndex.ColorB].AddKey(new Keyframe(time, spriteTimelineKey.Tint.b) { tangentMode = 0 }, lastTimelineKey);
160 obj.Curves[(int)AnimationCurveIndex.ColorA].AddKey(new Keyframe(time, spriteTimelineKey.Tint.a) { tangentMode = 0 }, lastTimelineKey);
161 obj.Curves[(int)AnimationCurveIndex.ZIndex].AddKey(new Keyframe(time, zIndex, float.PositiveInfinity, float.PositiveInfinity));
162 }
163 }
File name: AnimationCurveBuilder.cs
Copy
165 private ObjectCurves GetOrCreateAnimationCurves(string path)
166 {
167 ObjectCurves objCurves;
168
169 if (!curveCache.TryGetValue(path, out objCurves))
170 {
171 objCurves = new ObjectCurves();
172 objCurves.Curves = new AnimationCurve[(int)AnimationCurveIndex.ENUM_COUNT];
173 for (int i = 0; i < (int) AnimationCurveIndex.ENUM_COUNT; i++)
174 {
175 objCurves.Curves[i] = new AnimationCurve();
176 }
177 curveCache[path] = objCurves;
178 }
179 return objCurves;
180 }
Download file with original file name:AnimationCurve
AnimationCurve 97 lượt xem
Gõ tìm kiếm nhanh...