1.初始化animator,创建Playable图,创建动画Playable
private void InitAnimator(GameObject headGo)
{
if (headGo)
{
_headAnimator = headGo.GetComponent<Animator>();
if (_headAnimator)
{
_headAnimator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
}
else
{
_headAnimator = headGo.AddComponent<Animator>();
}
if(_headAnimator != null)
{
playableGraph = PlayableGraph.Create();
playableGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime); // 根据需要选择更新模式
// 创建一个输出节点
curPlayableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", _headAnimator);
}
}
}
2.播放动画
public void PlayExpression(string clipName,float time = 0)
{
// 加载动画片段
var clip = GetClip(clipName);
if (clip != null)
{
if (time > 0)
{
animaStartTime = Time.time;
isUpdate = true;
clipLength = clip.length;
loopNum = 1;
}
else
{
ResertAnimaParam();
}
// 将动画剪辑转换为Playable
curClipPlayable = AnimationClipPlayable.Create(playableGraph, clip);
//设置动画开始时间
curClipPlayable.SetTime(0);
// 将Playable连接到输出
curPlayableOutput.SetSourcePlayable(curClipPlayable);
playableGraph.Play();
}
else
{
#if UNITY_EDITOR
Debug.LogError("Expression clip not found: " + clipName);
#endif
}
}