Unity 动画帧采样

2024/01 08 10:01

美术可以制作一个复杂的Animation,程序只需要调用一下SetAniProgress,指定进度条就行了

using UnityEngine;

public class PlayFrame : MonoBehaviour
{
    public float rate = 0.35f;
    public string clipName = "Ani1";
    
    public static int SetAniProgress(GameObject obj, string clipName, float progress)
    {
        if (obj == null)
        {
            return -1;
        }

        var aniObj = obj.GetComponent<Animation>();
        if (aniObj == null)
        {
            return -2;
        }

        var clip = aniObj.GetClip(clipName);
        if (clip == null)
        {
            return -3;
        }

        var time = clip.length * progress;
        clip.SampleAnimation(obj, time);
        return 0;
    }

    void Update()
    {
        SetAniProgress(this.gameObject, clipName, rate);
    }
}