0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】TimelineのClipを生成した際の時間を設定する方法

Posted at

はじめに

image.png

UnityでTimelineを使用している際にClipを作成すると何もEditor拡張をしていない状態だと生成時間は5秒に設定されています。
このデフォルトの時間を好きな時間に変更する方法について解説します。

ソースコード

今回使用したコードはこちらにあります。

方法1 durationをoverrideする

自作したClipはPlayableAssetを継承していると思います。
PlayableAssetには

public virtual double duration => PlayableBinding.DefaultDuration;

があり、こちらをoverrideすることで生成したタイミング時で好きな時間にできます。

public class PrefabClip : PlayableAsset
{
    [SerializeField]
    private GameObject prefabA;

    [SerializeField]
    private GameObject prefabB;

    [SerializeField]
    private GameObject prefabC;

    // 試しに1秒に設定する
    public override double duration => 1.0;

    public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    {
        return ScriptPlayable<PrefabBehaviour>.Create(graph);
    }
}

image.png

この通り、1秒で作成されました。

image.png

しかし、このClipを横に伸ばすとクリックして選択した際に1秒以上の区間にHoldと表示がついてしまいます。

overrideする方法は手軽ですがこの設定を行う前とは別の見た目になってしまうので不便になるかもしれません。

方法2 Clipを作成した時点でdurationを決める

Editor拡張でClipを作成した時に好きな時間に設定します。

[CustomTimelineEditor(typeof(PrefabClip))]
public class PrefabClipEditor : ClipEditor
{
    private float _defaultDuration = 1.0f;

    public override void OnCreate(TimelineClip clip, TrackAsset track, TimelineClip clonedFrom)
    {
        base.OnCreate(clip, track, clonedFrom);

        // 作成するClipのDuration
        var createdClipDuration = clip.duration;

        // Unityのデフォルト時間ではない かつ 作ろうとしているClipの時間で無い場合はコピーしたClip
        if (createdClipDuration != TimelineClip.kDefaultClipDurationInSeconds && createdClipDuration != _defaultDuration)
        {
            clip.duration = createdClipDuration;
        }
        else
        {
            // コピーではない新規作成なので設定したい初期時間をSetする
            clip.duration = _defaultDuration;
        }
    }
}

OnCreateはClipを作成した時に呼ばれます。
こちらを使用し、Clipが作成されたタイミングでclip.durationに1.0を入れます。
しかし、その処理だけだとClipをコピーして貼り付けたときにコピーした時間ではなく、1.0で生成してしまうため

if (createdClipDuration != TimelineClip.kDefaultClipDurationInSeconds && createdClipDuration != _defaultDuration)

この処理でコピーしたClipかどうかを分岐します。
コピーしたClipである場合はclip.durationの時間はそのまま何もしないです。
コピーではない場合は新規作成になるのでclip.durationに1.0を入れます。

image.png

この方法で作成されたClipは方法1の時のようなHoldは表示されません。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?