Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active May 22, 2019 16:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsubaki/a8727e7177a29ce15d5ba72277a9a166 to your computer and use it in GitHub Desktop.
Save tsubaki/a8727e7177a29ce15d5ba72277a9a166 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
public class InsertAnimation : MonoBehaviour
{
private PlayableGraph graph;
private AnimationPlayableOutput output;
private Playable playable;
[SerializeField] AnimationCurve curve = null;
void Awake()
{
graph = PlayableGraph.Create ();
output = AnimationPlayableOutput.Create (graph, name, GetComponent<Animator> ());
output.SetWeight (0);
}
void OnDestroy()
{
graph.Destroy ();
}
public IEnumerator Play(float time, AnimationClip animClip)
{
if (playable.IsValid () )
playable.Destroy ();
// アニメーションのセットアップ
playable = AnimationClipPlayable.Create (graph, animClip);
output.SetSourcePlayable (playable);
graph.Play ();
// AnimatorからPlayableへ遷移
for(float endTime = Time.timeSinceLevelLoad + time;
endTime > Time.timeSinceLevelLoad; ){
float diff = 1 - (endTime - Time.timeSinceLevelLoad) / time;
output.SetWeight ( curve.Evaluate (diff));
yield return null;
}
output.SetWeight (1);
// PlayableからAnimatorへ
// AnimationClipPlayable再生中はAnimationControllerは止めておく
GetComponent<Animator>().playableGraph.Stop();
yield return new WaitForSeconds (animClip.length - time * 2);
GetComponent<Animator>().playableGraph.Play();
// PlayableからAnimatorへ
for(float endTime = Time.timeSinceLevelLoad + time;
endTime > Time.timeSinceLevelLoad; ){
float diff = (endTime - Time.timeSinceLevelLoad) / time;
output.SetWeight ( curve.Evaluate (diff));
yield return null;
}
output.SetWeight (0);
// アニメーションのクリーンナップ
playable.Destroy ();
graph.Stop ();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sample : MonoBehaviour
{
[SerializeField] InsertAnimation play = null;
private Coroutine coroutine;
public void Play(AnimationClip clip)
{
if( coroutine != null)
StopCoroutine (coroutine);
coroutine = StartCoroutine (play.Play (0.2f, clip));
}
}
@tsubaki
Copy link
Author

tsubaki commented Dec 20, 2017

animation 79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment