Skip to content

Instantly share code, notes, and snippets.

@baobao
Created January 28, 2025 02:07
Show Gist options
  • Save baobao/55f51fe611e2eef92827523258a4f52c to your computer and use it in GitHub Desktop.
Save baobao/55f51fe611e2eef92827523258a4f52c to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
public class MultiplePlayable : MonoBehaviour
{
[SerializeField] private AnimationClip _clip1;
[SerializeField] private AnimationClip _clip2;
[SerializeField] private Animator _animator;
[SerializeField] private Animator _animator2;
private AnimationMixerPlayable _mixerPlayable;
private PlayableGraph _graph;
[SerializeField, Range(0, 1f)] private float _weight = 0f;
void Awake()
{
// 1.PlayableGraphの作成
_graph = PlayableGraph.Create("MultiPlayableGraph");
// 2.Playable(インプット)の作成
// 2-1 AnimationMixerPlayableを作成
// 2つのPlayableが入力として接続されるので引数に「2」を指定
_mixerPlayable = AnimationMixerPlayable.Create(_graph, 2);
// ブレンドする2つのAnimationClipPlayableを作成
var playable1 = AnimationClipPlayable.Create(_graph, _clip1);
var playable2 = AnimationClipPlayable.Create(_graph, _clip2);
// 2-2.Playable同士の接続(mixerPlayableにplayable1とplayable2を接続)
_mixerPlayable.ConnectInput(0, playable1, 0);
_mixerPlayable.ConnectInput(1, playable2, 0);
// 2-3.ブレンド率を設定
_mixerPlayable.SetInputWeight(0, 1f);
_mixerPlayable.SetInputWeight(1, 0f);
// 3.PlayableOutput(アウトプット)の作成
var output = AnimationPlayableOutput.Create(_graph, "AnimationOutput", _animator);
var output2 = AnimationPlayableOutput.Create(_graph, "AnimationOutput", _animator2);
// 4.PlayableOutputにPlayableを接続
output.SetSourcePlayable(_mixerPlayable);
output2.SetSourcePlayable(playable2);
// 5.PlayableGraphを通して再生
_graph.Play();
}
void Update()
{
// ブレンド率を更新
_mixerPlayable.SetInputWeight(0, _weight);
_mixerPlayable.SetInputWeight(1, 1f - _weight);
}
void OnDestroy()
{
// 6.使い終わったらPlayableGraphを破棄
_graph.Destroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment