2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

自分がUnity Timelineのカスタムクリップを作るときに使いたいTrack、Mixerを含むテンプレート

Last updated at Posted at 2022-12-24

概要

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);
    }
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?