概要
UnityのTimelineでカスタムクリップを作るときにAnimationKeyが使えてMixerで制御する前提のテンプレートです。
webの記事ではClipとPlayableBehaviourまではでてきますが、Track、Mixerまであまり出てこないのですべて含めてシンプルにまとめました。
Clip
ExampleClip.cs
using System;
using UnityEngine;
using UnityEngine.Playables;
[Serializable]
public class ExampleClip : PlayableAsset
{
public ExamplePlayableBehaviour behaviour = new ExamplePlayableBehaviour();
public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
{
ScriptPlayable<ExamplePlayableBehaviour> playable =
ScriptPlayable<ExamplePlayableBehaviour>.Create(graph, behaviour);
return playable;
}
}
PlayableBehaviour
ExamplePlayableBehaviour.cs
using System;
using UnityEngine.Playables;
[Serializable]
public class ExamplePlayableBehaviour : PlayableBehaviour
{
public float SampleValue;
}
Mixer
ExampleMixer.cs
using UnityEngine;
using UnityEngine.Playables;
public class ExampleMixer : PlayableBehaviour
{
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
float value = 0.0f;
for (int i = 0; i < playable.GetInputCount(); i++)
{
var sp = (ScriptPlayable<ExamplePlayableBehaviour>)playable.GetInput(i);
var behaviour = sp.GetBehaviour();
var weight = playable.GetInputWeight(i);
value += behaviour.SampleValue * weight;
}
Debug.Log($"SampleValie:{value}");
}
}
Track
ExampleTrack.cs
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
[TrackClipType(typeof(ExampleClip))]
public class ExampleTrack : TrackAsset
{
public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
{
return ScriptPlayable<ExampleMixer>.Create(graph, inputCount);
}
}