Playable API - 播放单个Animation Clip

用到的模型资源:GitHub - unity3d-jp/unitychan-crs: Unity-Chan "Candy Rock Star" Live Demo

这边直接在他提供的Scene上修改

思路:AnimationClipPlayable可以关联单个AnimationClip,这边AnimationPlayableOutput从AnimationClipPlayable接收输入,然后输出到PlayableGraph。

using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

public class PlayAnimClip : MonoBehaviour
{
    public AnimationClip animClip;

    private PlayableGraph _graph;
    private AnimationClipPlayable _animClipPlayable;

    void Start()
    {
        _graph = PlayableGraph.Create("ChanPlayableGraph");
        var animationOutputPlayable = AnimationPlayableOutput.Create(_graph, "AnimationOutput", GetComponent<Animator>()); //往graph添加output

        _animClipPlayable = AnimationClipPlayable.Create(_graph, animClip); //往graph添加playable
        animationOutputPlayable.SetSourcePlayable(_animClipPlayable);
        _graph.Play();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            _animClipPlayable.SetTime(0);
        }
    }

}

把本来的几个脚本都关掉,添加上面的PlayAnimClip脚本,再把向前跑动画绑定上去。

Animator不能关掉,Playable动画的播放还是会用到他,清掉Controller上的绑定。

运行效果:按1的时候从头开始播放

看下PlayableGraph的可视化图形:

 

按0.1s的步进让动画一个一个画面的播放

void Update()
{
    //按0.1s的步进让动画一个一个画面的播放
    if (Input.GetMouseButtonDown(0))
    {
        if (PlayState.Paused != _animClipPlayable.GetPlayState())
            _animClipPlayable.Pause();

        _time += 0.1f;
        _animClipPlayable.SetTime(_time);
    }
}

点击一次播一个画面

 

使用注意 

a) _animClipPlayable.GetTime()

Paused了时间才会停住, 不然时间会一直累加,就算播完了

b) _animClipPlayable.IsDone()

非循环的动画播完了也不会设为true的, 要我们自己设置

 

Playable常用函数

playable.GetPlayState();
playable.Pause();
playable.Play();

playable.GetSpeed();
playable.SetSpeed(value);

playable.GetDuration();
playable.SetDuration(d);

playable.GetTime();
playable.ResetTime(time);

playable.IsDone();
playable.SetDone(b);

playable.GetInputCount();
playable.SetInputCount(count);

playable.GetInputWeight(index);
playable.SetInputWeight(index, weight);

 

参考
【Unity】简单使用Playable API控制动画 - 知乎 (zhihu.com)

posted @ 2022-12-18 00:59  yanghui01  阅读(304)  评论(0编辑  收藏  举报