Last active
May 22, 2019 16:16
-
-
Save tsubaki/a8727e7177a29ce15d5ba72277a9a166 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; | |
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 (); | |
} | |
} |
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; | |
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)); | |
} | |
} |
Author
tsubaki
commented
Dec 20, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment