clips | pre-registers animation clips |
pre-registers animation clips with the Animator for optimized.
This method allows you pre-registers animation clips with the Animator for optimized Playables performance. Required before using SetSourcePlayableWithoutRebind to cache clips and enable seamless transitions.
Only support on TuanjieAnimation
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Animations; using UnityEngine.Experimental.Animations; using UnityEngine.Playables;
public class SinglePlayableSwitch : MonoBehaviour { public AnimationClip clip1; public AnimationClip clip2;
private PlayableGraph playableGraph; private AnimationPlayableOutput playableOutput; private AnimationClipPlayable clipPlayable1; private AnimationClipPlayable clipPlayable2;
private float timer = 0f; private bool isPlayingClip1 = true;
void Start() { if (clip1 == null || clip2 == null) { Debug.LogError("Inspector two AnimationClip!"); return; }
// Create PlayableGraph playableGraph = PlayableGraph.Create("AnimationSwitcherGraph");
// Get Animator and create playableoutput Animator animator = GetComponent<Animator>(); if (animator == null) { Debug.LogError("Animator not Found!"); return; }
playableOutput = AnimationPlayableOutput.Create(playableGraph, "AnimationOutput", animator);
clipPlayable1 = AnimationClipPlayable.Create(playableGraph, clip1); clipPlayable2 = AnimationClipPlayable.Create(playableGraph, clip2);
/// AnimationClip[] clips = new AnimationClip[2]; clips[0] = clip1; clips[1] = clip2; animator.SetupAnimationClipsUserCache(clips); animator.Rebind();
playableOutput.SetSourcePlayableWithoutRebind(clipPlayable1); playableGraph.Play(); }
void Update() { timer += Time.deltaTime;
if (timer >= 0.5f) { timer = 0f;
if (isPlayingClip1) { playableOutput.SetSourcePlayableWithoutRebind(clipPlayable2); } else { playableOutput.SetSourcePlayableWithoutRebind(clipPlayable1); }
isPlayingClip1 = !isPlayingClip1; } }
void OnDestroy() { if (playableGraph.IsValid()) { playableGraph.Destroy(); } } }