Last active
June 25, 2019 07:16
-
-
Save tsubaki/24565b08cc8c0ec3b1614b7bf6edaa5b to your computer and use it in GitHub Desktop.
Animatorでアニメーションを再生する
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))] | |
[DisallowMultipleComponent] | |
public class SimpleAnimator : MonoBehaviour | |
{ | |
PlayableGraph graph; | |
AnimationMixerPlayable mixer; | |
AnimationClipPlayable prePlayable, currentPlayable; | |
Coroutine coroutinePlayAnimation; | |
public List<AnimationClip> clipList; | |
void Awake() | |
{ | |
graph = PlayableGraph.Create(name); | |
AnimationPlayableOutput.Create(graph, name, GetComponent<Animator>()); | |
} | |
void OnDestroy() | |
{ | |
graph.Destroy(); | |
} | |
void Start() | |
{ | |
mixer = AnimationMixerPlayable.Create(graph, 2, true); | |
var output = graph.GetOutput(0); | |
output.SetSourcePlayable(mixer); | |
if (clipList.Count != 0) | |
{ | |
currentPlayable = AnimationClipPlayable.Create(graph, clipList[0]); | |
mixer.ConnectInput(0, currentPlayable, 0); | |
mixer.SetInputWeight(0, 1); | |
graph.Play(); | |
} | |
} | |
public void CrossFade(string animation, float fadeLength) | |
{ | |
CrossFade(clipList.Find((c) => c.name == animation), fadeLength); | |
} | |
public void CrossFade(string animation) | |
{ | |
CrossFade(animation, 0.3f); | |
} | |
public void CrossFade(AnimationClip clip) | |
{ | |
CrossFade(clip, 0.3f); | |
} | |
public void CrossFade(AnimationClip clip, float fadeLength) | |
{ | |
if (coroutinePlayAnimation != null) | |
StopCoroutine(coroutinePlayAnimation); | |
coroutinePlayAnimation = StartCoroutine(PlayAnimation(clip, fadeLength)); | |
} | |
public void Play(string clipName) | |
{ | |
Play(clipList.Find((c) => c.name == clipName)); | |
} | |
public void Play(AnimationClip newAnimation) | |
{ | |
if (currentPlayable.IsValid()) | |
currentPlayable.Destroy(); | |
DisconnectPlayables(); | |
currentPlayable = AnimationClipPlayable.Create(graph, newAnimation); | |
mixer.ConnectInput(0, currentPlayable, 0); | |
mixer.SetInputWeight(1, 0); | |
mixer.SetInputWeight(0, 1); | |
} | |
void DisconnectPlayables() | |
{ | |
graph.Disconnect(mixer, 0); | |
graph.Disconnect(mixer, 1); | |
if (prePlayable.IsValid()) | |
prePlayable.Destroy(); | |
} | |
IEnumerator PlayAnimation(AnimationClip newAnimation, float transitionTime) | |
{ | |
DisconnectPlayables(); | |
prePlayable = currentPlayable; | |
currentPlayable = AnimationClipPlayable.Create(graph, newAnimation); | |
mixer.ConnectInput(1, prePlayable, 0); | |
mixer.ConnectInput(0, currentPlayable, 0); | |
// 指定時間でアニメーションをブレンド | |
float waitTime = Time.timeSinceLevelLoad + transitionTime; | |
yield return new WaitWhile(() => { | |
var diff = waitTime - Time.timeSinceLevelLoad; | |
if (diff <= 0) | |
{ | |
mixer.SetInputWeight(1, 0); | |
mixer.SetInputWeight(0, 1); | |
return false; | |
} | |
else | |
{ | |
var rate = Mathf.Clamp01(diff / transitionTime); | |
mixer.SetInputWeight(1, rate); | |
mixer.SetInputWeight(0, 1 - rate); | |
return true; | |
} | |
}); | |
coroutinePlayAnimation = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment