纳金网

标题: AnimationCurve 用法 [打印本页]

作者: 烟雨    时间: 2015-8-30 00:16
标题: AnimationCurve 用法
今天发现这个函数,查了一下,我觉得非常实用.编辑一条任意变化的曲线用在任何你想用在的地方。 如曲线地形,曲线轨迹.基本用法是:
1.创建物体.
2.创建脚本.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public AnimationCurve anim;
}

然后你就能在Inspector中看到 164557gr0k6sh8y61rc76m.png ,双击绿色线那个窗口你就可以进行编辑.进去怎么用就自己研究吧.

AnimationCurve可以理解为2部分。(1)键序列(2)左右循环模式(又作左右包裹模式)

一:键序列
创建键序列:Keyframe[] ks = new Keyframe[3];
曲线中加入键序列:AnimationCurve curve = new AnimationCurve(ks);
获取曲线中的键序列:curve[index]   或者 curve.keys[index]
添加单键:curve.Addkey(time,value)
删除单键:curve.RemoveKey(index)
二:左右循环
anim.preWrapMode = WrapMode.Loop;
anim.postWrapMode = WrapMode.Once;
三:键
Keyframe kf = new Keyframe(time,value);
kf.inTangent = 45;
kf.outTangent = 45;


例子就丢官方代码上来了:
public AnimationCurve anim;
    private Keyframe[] ks;
    void Start()
    {
        ks = new Keyframe[50];
        int i = 0;
        while (i < ks.Length)
        {
            ks = new Keyframe(i, i * i);
            i++;
        }
        anim = new AnimationCurve(ks);
    }

    void Update()
    {
        transform.position = new Vector3(Time.time, anim.Evaluate(Time.time), 0);
    }

我说下效果吧,就是物体一直斜向上升.





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5