概述
本篇介绍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>(