Last active
June 25, 2019 08:20
-
-
Save tsubaki/50c245be4a5e1da4c1ef22dc00250a33 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 LayerAnimation : MonoBehaviour { | |
PlayableGraph graph; | |
AnimationMixerPlayable mixer; | |
[SerializeField] AnimationClip runClip, face1Clip, face2Clip; | |
[Range(0, 1)] public float weight; | |
void Awake() | |
{ | |
graph = PlayableGraph.Create (); | |
} | |
void Start() | |
{ | |
// アニメーションをResourcesから取得し | |
// AnimationClipPlayableを構築 | |
var runPlayable = AnimationClipPlayable.Create (graph, runClip); | |
var face1Playable = AnimationClipPlayable.Create (graph, face1Clip); | |
var face2Playable = AnimationClipPlayable.Create (graph, face2Clip); | |
// 表情を二つ登録 | |
mixer = AnimationMixerPlayable.Create (graph, 2, true); | |
mixer.ConnectInput (0, face1Playable, 0); | |
mixer.ConnectInput (1, face2Playable, 0); | |
// runPlayable(走るモーション)とmixer(表情)を合成 | |
var layer = AnimationLayerMixerPlayable.Create (graph, 2); | |
layer.AddInput (runPlayable, 0, 1); | |
layer.AddInput (mixer, 0, 1); | |
// outputを生成して、出力先を自身のAnimatorに設定 | |
var output = AnimationPlayableOutput.Create (graph, "output", GetComponent<Animator>()); | |
// playableをoutputに流し込む | |
output.SetSourcePlayable (layer); | |
graph.Play (); | |
} | |
void Update() | |
{ | |
mixer.SetInputWeight (0, weight); | |
mixer.SetInputWeight (1, 1 - weight); | |
} | |
void OnDestroy() | |
{ | |
graph.Destroy (); | |
} | |
} |
Author
tsubaki
commented
Jul 29, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment