Unity自定义播放控制(二)——Playables示例

概述

本篇介绍Playables应用示例

PlayableGraph可视化工具

PlayableGraph Visualizer可以实现Playable Graph的可视化,这个可是我们的辅助利器
Git地址: https://github.com/Unity-Technologies/graph-visualizer
在这里插入图片描述
使用步骤:
1. 下载工具
2. 在Unity中通过Window-Analysis-PlayableGraph Visualizer打开工具
通过GraphVisualizerClient.Show(PlayableGraph graph, string name)接口打开我们的Graph(或者运行也会查找出所有的Graph)

其中线条的颜色深度代表了混合权重

示例1:简单播放一个动画


using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class PlayAnimationSample : MonoBehaviour
{
   
    public Animator animator;
    public AnimationClip clip;
    PlayableGraph playableGraph;
    void Start()
    {
   
        if (animator == null)
            animator = GetComponent<Animator>();
        // 创建PlayableGraph
        playableGraph = PlayableGraph.Create("PlayAnimationSample");
        playableGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
        // 创建Playable
        AnimationClipPlayable playable = AnimationClipPlayable.Create(playableGraph, clip);
        // 创建PlayableOutput
        AnimationPlayableOutput playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", animator);
        // 链接PlayableOutput和Playable
        playableOutput.SetSourcePlayable(playable);
        playableGraph.Play();
    }
    void OnDestroy()
    {
   
        // 要记得销毁
        playableGraph.Destroy();
    }
}

在这里插入图片描述
在这里插入图片描述
我们还可以使用AnimationPlayableUtilities.PlayClip()非常方便的播放动画

using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
[RequireComponent(typeof(Animator))]
public class PlayAnimationUtilitiesSample : MonoBehaviour
{
   
    public Animator animator;
    public AnimationClip clip;
    PlayableGraph playableGraph;
    void Start()
    {
   
        if(animator==null)
            animator = GetComponent<Animator>();
        // playableGraph = PlayableGraph.Create("PlayAnimationUtilitesSample");
        // 这个方法会产生一个新的playableGraph,所以我们无需提前创建,创建后会导致存在两个Graph,可以通过Visualizer看出来
        AnimationPlayableUtilities.PlayClip(animator, clip, out playableGraph);
    }
    void OnDestroy()
    {
   
        playableGraph.Destroy();
    }
}

在这里插入图片描述
在这里插入图片描述

示例2:混合动画

我们可以使用AnimationMixerPlayable来混合两个动画片段


using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
/// <summary>
/// 动画混合
/// </summary>
[RequireComponent(typeof(Animator))]
public class MixAnimationSample : MonoBehaviour
{
   
    public Animator animator;
    public AnimationClip clip0, clip1;
    public float weight;
    PlayableGraph graph;
    AnimationMixerPlayable mixerPlayable;
    void Start()
    {
   
        if (animator == null)
            animator = GetComponent<Animator>(
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值