问题
- 我需要通过脚本设置动画曲线插值。
原因
您用脚本设置动画曲线,当插值完成后,可以得到如下结果:
解决方案
为了解决问题,请按照下面两步进行:
- 在脚本中手动设置动画曲线属性后,调用AnimationClip.EnsureQuaternionContinuity 。
- 使用AssetDatabase.SaveAssets保存资源。
请参考下述例程:
AnimationClip clip = new AnimationClip ();
//If using Unity 5
clip.legacy = true;
// Rotation
AnimationCurve CurveRot = new AnimationCurve ();
AnimationCurve CurveRotY = new AnimationCurve ();
AnimationCurve CurveRotZ = new AnimationCurve ();
AnimationCurve CurveRotW = new AnimationCurve ();
float time = 0.5f;
//stepValues [0,1,0,10]
Quaternion angle = Quaternion.Euler (0, 0, stepValues[i] * 180.0f);
CurveRot.AddKey (time, angle.x);
CurveRotY.AddKey (time, angle.y);
CurveRotZ.AddKey (time, angle.z);
CurveRotW.AddKey (time, angle.w);
clip.SetCurve ("", typeof(Transform),"localRotation.z", CurveRot);
clip.SetCurve ("",typeof(Transform),"localRotation.y", CurveRotY);
clip.SetCurve ("",typeof(Transform),"localRotation.z", CurveRotZ);
clip.SetCurve ("",typeof(Transform),"localRotation.w", CurveRotW);
//This ensures a smooth interpolation
clip.EnsureQuaternionContinuity ();
String newCName = "Comp1_L_1";
AssetDatabase.CreateAsset (clip,ANIM_CLIP_PATH+newCName+".anim");
AssetDatabase.SaveAssets ();
更多信息
http://docs.unity3d.com/Manual/animeditor-AnimationCurves.html
本文适合于Unity 5.x版本