Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created July 29, 2017 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsubaki/ae2af549b8269c2d2d11bff81901deab to your computer and use it in GitHub Desktop.
Save tsubaki/ae2af549b8269c2d2d11bff81901deab to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
public class MixAnimation : MonoBehaviour {
PlayableGraph graph;
AnimationMixerPlayable mixer;
[SerializeField] string clipName1 = "RobotBoyIdle", clipName2 = "RobotBoyRun";
[Range(0, 1)] public float weight;
void Awake()
{
graph = PlayableGraph.Create ();
}
void Start()
{
// アニメーションをResourcesから取得し
// AnimationClipPlayableを構築
var clip1 = Resources.Load<AnimationClip> (clipName1);
var clip1Playable = AnimationClipPlayable.Create (graph, clip1);
var clip2 = Resources.Load<AnimationClip> (clipName2);
var clip2Playable = AnimationClipPlayable.Create (graph, clip2);
// ミキサーを生成して、Clip1とClip2を登録
// (代わりにAnimatorControllerPlayableとかでも可能)
mixer = AnimationMixerPlayable.Create (graph, 2, true);
mixer.ConnectInput (0, clip1Playable, 0);
mixer.ConnectInput (1, clip2Playable, 0);
// outputを生成して、出力先を自身のAnimatorに設定
var output = AnimationPlayableOutput.Create (graph, "output", GetComponent<Animator>());
// playableをoutputに流し込む
output.SetSourcePlayable (mixer);
graph.Play ();
}
void Update()
{
mixer.SetInputWeight (0, weight);
mixer.SetInputWeight (1, 1 - weight);
}
void OnDestroy()
{
graph.Destroy ();
}
}
@tsubaki
Copy link
Author

tsubaki commented Jul 29, 2017

animation 97

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