Version: 1.7
LanguageEnglish
  • C#

Animator.SetupAnimationClipsUserCache

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Declaration

public void SetupAnimationClipsUserCache(AnimationClip[] clips);

Parameters

clips pre-registers animation clips

Description

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(); } } }