http://blog.sina.com.cn/s/blog_471132920101f8nv.html
说明:
关于animationCurve的使用总结
1 创建,可以新建脚本,创建animationCurve变量,然后直接在脚本上手动拖动可视化创建曲线,也可以用代码创建(这种比较不直观)
2 使用,anim.Evaluate(Time.time)
3 存储,编辑器手动创建曲线,然后代码中读取各项key数据,二进制保存起来,用的时候再读取
原创文章如需转载请注明:转载自风宇冲Unity3D教程学院
using UnityEngine; using System.Collections; public class AnimationCurveTutor : MonoBehaviour { public AnimationCurve anim; }
using UnityEngine; using System.Collections; public class AnimationCurveTutor : MonoBehaviour { public AnimationCurve anim; public void Update() { transform.position = new Vector3(Time.time, anim.Evaluate(Time.time), 0); } }
一:键序列
创建键序列:Keyframe[] ks = new Keyframe[3];
曲线中加入键序列:AnimationCurve curve = new AnimationCurve(ks);
获取曲线中的键序列:curve[index] 或者 curve.keys[index]
添加单键:curve.Addkey(time,value)
anim.postWrapMode = WrapMode.Once;
三:键
Keyframe kf = new Keyframe(time,value);
kf.inTangent = 45;
kf.outTangent = 45;
using UnityEngine; using System.Collections; public class CreateRuntime : MonoBehaviour { public AnimationCurve anim = new AnimationCurve(); void Start() { Keyframe[] ks = new Keyframe[3]; ks[0] = new Keyframe(0, 0); ks[0].inTangent = 0; ks[1] = new Keyframe(4, 0); ks[1].inTangent = 45; ks[2] = new Keyframe(8, 0); ks[2].inTangent = 90; anim = new AnimationCurve(ks); } void Update() { transform.position = new Vector3(Time.time, anim.Evaluate(Time.time), 0); } }
-
using UnityEngine; using System.Collections; using UnityEditor; public class EditorCurves :EditorWindow { AnimationCurve curveX = AnimationCurve.Linear(0,0,10,10); [MenuItem("Examples/Mine")] static void Init() { EditorWindow window = GetWindow(typeof(EditorCurves)); window.position = new Rect(0,300,200,100); window.Show(); } void OnGUI() { curveX = EditorGUI.CurveField(new Rect(3,3,position.width-6,15), "Animation on X", curveX); if(GUI.Button(new Rect(3,60,position.width-6,30),"Generate Curve")) AddCurveToSelectedGameObject(); } void AddCurveToSelectedGameObject() { if(Selection.activeGameObject) { FollowAnimationCurveMine comp = Selection.activeGameObject.AddComponent<FollowAnimationCurveMine>(); comp.SetCurves(curveX); } else { Debug.LogError("No Game Object selected for adding an animation curve"); } } }
using UnityEngine; using System.Collections; public class FollowAnimationCurveMine : MonoBehaviour { public AnimationCurve curveX; public void SetCurves(AnimationCurve tmpCurve) { curveX = tmpCurve; } void Update () { if(curveX != null) transform.position = new Vector3(1f*Time.time, curveX.Evaluate(Time.time) * 0.3f,0); } }
© 著作权归作者所有
发表评论