1.模型身上挂脚本和Animation clips
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
public class AnimationClipQueuePlayable : PlayableBehaviour
{
public AnimationMixerPlayable mixer;
public float playedTime;
private int currentClipIndex;
public void Init(AnimationClip[] clips,Playable owner,PlayableGraph graph)
{
// 输出的接收者只有一个
owner.SetInputCount(1);
mixer = AnimationMixerPlayable.Create(graph, clips.Length);
graph.Connect(mixer, 0, owner, 0);
owner.SetInputWeight(0, 1);
for (int i = 0; i < clips.Length; i++)
{
graph.Connect(AnimationClipPlayable.Create(graph,clips[i]),0,mixer,i); ;
mixer.SetInputWeight(i, 1);
}
}
public override void PrepareFrame(Playable playable,FrameData info)
{
if (mixer.GetInputCount() == 0)
{
return;
}
playedTime += info.deltaTime;
var currentClip = (AnimationClipPlayable)mixer.GetInput(currentClipIndex);
if (playedTime >= currentClip.GetAnimationClip().length)
{
currentClipIndex++;
if (currentClipIndex >= mixer.GetInputCount())
{
currentClipIndex = 0;
}
playedTime = 0;
currentClip = (AnimationClipPlayable)mixer.GetInput(currentClipIndex);
currentClip.SetTime(0);
}
for (int i = 0; i < mixer.GetInputCount(); i++)
{
if (i == currentClipIndex)
{
mixer.SetInputWeight(i, 1);
}
else
mixer.SetInputWeight(i, 0);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
public class PlayAnimationClips : MonoBehaviour
{
private PlayableGraph PlayableGraph;
public AnimationClip[] clips;
// Start is called before the first frame update
void Start()
{
PlayableGraph = PlayableGraph.Create("PlayAnimationClips");
var output = AnimationPlayableOutput.Create(PlayableGraph, "Animations", GetComponent<Animator>());
var playQueuePlayable = ScriptPlayable<AnimationClipQueuePlayable>.Create(PlayableGraph);
var playQueue = playQueuePlayable.GetBehaviour();
playQueue.Init(clips, playQueuePlayable, PlayableGraph);
output.SetSourcePlayable(playQueuePlayable);
PlayableGraph.Play();
}
private void OnDestroy()
{
PlayableGraph.Destroy();
}
}