Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created January 9, 2018 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/033f68ac6af60161d2946696db6a55c3 to your computer and use it in GitHub Desktop.
Save tsubaki/033f68ac6af60161d2946696db6a55c3 to your computer and use it in GitHub Desktop.
単純なアニメーションの割り込み再生
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
using UnityEngine.Timeline;
[RequireComponent (typeof(Animator))]
public class PlaySingleAnimation : MonoBehaviour
{
PlayableGraph graph;
void OnEnable ()
{
var animator = GetComponent<Animator> ();
if (animator.runtimeAnimatorController == null) {
graph = GetComponent<Animator> ().playableGraph;
} else {
graph = PlayableGraph.Create ();
var output = AnimationPlayableOutput.Create (graph, name, animator);
}
}
void OnDisable()
{
graph.Destroy ();
}
public void Play (AnimationClip newAnimation)
{
StartCoroutine(PlayAnimation(newAnimation));
}
public IEnumerator PlayAnimation (AnimationClip newAnimation)
{
var playable = AnimationClipPlayable.Create (graph, newAnimation);
var output = graph.GetOutput (0);
output.SetSourcePlayable (playable);
graph.Play ();
yield return new WaitForSeconds (newAnimation.length);
graph.DestroyPlayable (playable);
graph.Stop ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment