Playable API:定制你的动画系统 简单使用

前言

有关Playable的介绍,官方有篇中文的文章,大家可以优先看一下,这里就不过多描述了。

文章链接:https://connect.unity.com/p/playable-api-ding-zhi-ni-de-dong-hua-xi-tong

官方Demo:https://github.com/Unity-Technologies/SimpleAnimation

Playables API:https://docs.unity3d.com/Manual/Playables.html

 

准备

由于是动画系统嘛,所以肯定要有动画。我们先在场景中创建一个Cube,然后选中Cube在Animation面板中为其创建几个简单动画即可(当然了,有现成资源的小伙伴可以跳过这步了),Demo中我创建了三个名为Jump,Rotate,Scale的Animation文件。同时由于刚刚Create Animation的操作,Unity会在我们的Cube上自动添加Animator组件,并且关联了一个Animator的Controller文件。Animator组件需要保留(驱动Playable Graph的实际上依然是Animator组件),但是Controller我们暂时用不到,先删除它。

            

同时Unity提供了一个查看Playable结构的工具:PlayableGraph Visualizer,我们打开Package Manager,在Advanced中选中Show Preview Packages,然后找到PlayableGraph Visualizer,下载它。下载好后可以在Window-Analysis-PlayaleGraph Visualizer打开它。

 

AnimationPlayable

接下来自然是要利用Playable使我们的Cube播放动画了,我们先创建一个脚本组件(PlayableTest)挂载在Cube上。

我们先创建几个AnimationClip变量用于关联我们的Animation文件

public AnimationClip jumpAnimationClip;
public AnimationClip rotateAnimationClip;
public AnimationClip scaleAnimationClip;

AnimationPlayableOutput与AnimationClipPlayable

接下里我们来看看一个最简单的AnimationPlayable的实现

PlayableGraph m_graph;

void Start()
{
    m_graph = PlayableGraph.Create("TestPlayableGraph");
    
    var animationOutputPlayable = AnimationPlayableOutput.Create(m_graph, "AnimationOutput", GetComponent<Animator>());
    var jumpAnimationClipPlayable = AnimationClipPlayable.Create(m_graph, jumpAnimationClip);
    //AnimationPlayableOutput只有一个输入口,所以port为0
    animationOutputPlayable.SetSourcePlayable(jumpAnimationClipPlayable, 0);

    m_graph.Play();
}

void OnDisable()
{
    // 销毁graph中所有的Playables和PlayableOutputs
    m_graph.Destroy();
}

PlayableGraph类似于一个Playable的容器,我们往里面添加了一个用做动画输出的AnimationPlayableOutput和一个关联动画的AnimationClipPlayable,并用SetSourcePlayable将其关联起来(一个PlayableOutput只能有一个SourcePlayable)。

运行后,就可以看见我们的Cube播放了我们设置的Jump动画,同时查看PlayaleGraph Visualizer来更直观的了解,如图:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值