Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
AnimationControllerのアニメーションに割り込み
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
public class Warikomi : MonoBehaviour {
PlayableGraph graph;
AnimatorControllerPlayable controllerPlayable;
AnimationMixerPlayable mixer;
[SerializeField] AnimationClip clip = null; // 特殊アニメーション
[SerializeField] RuntimeAnimatorController controller = null;
[SerializeField, Range(0, 1)] float speed = 1;
void Awake()
{
graph = GetComponent<Animator> ().playableGraph;
}
IEnumerator Start ()
{
// AnimationControllerのPlayableを取得して、Mixerに登録
controllerPlayable = AnimatorControllerPlayable.Create (graph, controller);
mixer = AnimationMixerPlayable.Create (graph, 2, true);
mixer.ConnectInput (0, controllerPlayable, 0);
mixer.SetInputWeight (0, 1);
GetComponent<Animator> ().playableGraph.GetOutput (0).SetSourcePlayable (mixer);
// スペースキーが押されるまで待つ
yield return new WaitUntil (() => Input.GetKeyDown (KeyCode.Space));
// Playableを登録
var playable = AnimationClipPlayable.Create (graph, clip);
mixer.ConnectInput (1, playable, 0);
// アニメーションを切替、終了まで待ち、AnimationControllerに戻す
yield return Mix(0.3f, 0, 1);
yield return new WaitForSeconds (clip.length - 0.3f);
yield return Mix(0.3f, 1, 0);
// 不要なPlayableを破棄
graph.Disconnect (mixer, 1);
playable.Destroy ();
}
IEnumerator Mix(float time, int fromInput, int toInput)
{
var waitTime = Time.timeSinceLevelLoad + time;
yield return new WaitWhile (() => {
var diff = waitTime - Time.timeSinceLevelLoad;
if( diff <= 0){
mixer.SetInputWeight(fromInput, 0);
mixer.SetInputWeight(toInput, 1);
return false;
}
else{
var rate = Mathf.Clamp01(diff / time);
mixer.SetInputWeight(fromInput, rate);
mixer.SetInputWeight(toInput, 1- rate);
return true;
}
});
}
void Update()
{
controllerPlayable.SetFloat ("Speed", speed);
}
void OnDestroy()
{
graph.Destroy ();
}
}
Owner

tsubaki commented Jul 29, 2017

animation 102

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