Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created December 20, 2017 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/1216e3d45d268d3a6f704f7ed312f57b to your computer and use it in GitHub Desktop.
Save tsubaki/1216e3d45d268d3a6f704f7ed312f57b to your computer and use it in GitHub Desktop.
稼働中のアニメーションコントローラーにAnimationClipPlayableを混ぜる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class BlendAnimationController2Playable : MonoBehaviour
{
private PlayableGraph graph, animControlGraph;
private AnimationPlayableOutput output;
[SerializeField] AnimationClip clip = null;
void Awake()
{
// AnimationControllerのPlayableGraphは既に稼働しているので、新しく作る
graph = PlayableGraph.Create ();
// アニメーション再生の出力先を登録
var animator = GetComponent<Animator> ();
output = AnimationPlayableOutput.Create (graph, name, animator);
output.SetSourcePlayable (AnimationClipPlayable.Create (graph, clip));
// AnimationController側の操作用にキャッシュしとく
animControlGraph = animator.playableGraph;
}
void OnDestroy()
{
// PlayableGraphは必ず破棄する
graph.Destroy ();
}
public void SetWeight(float value)
{
// AnimationControllerがフルに動いてる時はPlayableGraphは停止させる
if (value == 0) {
graph.Stop ();
} else if( graph.IsPlaying() == false){
graph.Play ();
}
// Playable Graphがフルに動いてる時はAnimationControllerは停止させる
if (value == 1) {
animControlGraph.Stop ();
} else if (animControlGraph.IsPlaying () == false) {
animControlGraph.Play ();
}
// PlayableGraphのブレンド率
output.SetWeight (value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment