Created
December 20, 2017 12:08
-
-
Save tsubaki/1216e3d45d268d3a6f704f7ed312f57b to your computer and use it in GitHub Desktop.
稼働中のアニメーションコントローラーにAnimationClipPlayableを混ぜる
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; | |
[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