Created
January 9, 2018 14:44
-
-
Save tsubaki/033f68ac6af60161d2946696db6a55c3 to your computer and use it in GitHub Desktop.
単純なアニメーションの割り込み再生
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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