Skip to content

Instantly share code, notes, and snippets.

@PopupAsylumUK
Created February 13, 2018 09:53
Show Gist options
  • Save PopupAsylumUK/bc37a3d8d02f9a5c274637e503743c21 to your computer and use it in GitHub Desktop.
Save PopupAsylumUK/bc37a3d8d02f9a5c274637e503743c21 to your computer and use it in GitHub Desktop.
Plays an animation on an Animator automatically
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
public class PlayAutomatically : MonoBehaviour {
[SerializeField]
AnimationClip modelAnimationClip;
PlayableGraph playableGraph;
float playableGraphTime;
[SerializeField]
bool playAutomatically = true;
private void OnEnable() {
if (playAutomatically) {
CreateAnimationGraph();
playableGraph.Play();
}
}
void CreateAnimationGraph() {
playableGraph = PlayableGraph.Create();
playableGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
var clipPlayable = AnimationClipPlayable.Create(playableGraph, modelAnimationClip);
playableOutput.SetSourcePlayable(clipPlayable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment