2
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?

More than 1 year has passed since last update.

ループ再生するクリップの作り方

Last updated at Posted at 2023-07-03

2つやり方がある

  1. OnBehaviourPauseでクリップの再生時間を減算する
  2. OnBehaviourPauseでクリップの開始時間を代入する

まず、再生時間を減算するやり方

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

public class LoopBehaviour : PlayableBehaviour
{
    public bool loop = true;

    public override void OnBehaviourPause(Playable playable, FrameData info)
    {
        if (!loop) return;
        
        if(playable.GetGraph().GetResolver() is PlayableDirector director)
            director.time -= playable.GetDuration();
    }
}

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

public class LoopClip : PlayableAsset, ITimelineClipAsset
{
    public bool loop = true;

    ClipCaps ITimelineClipAsset.clipCaps { get; }

    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        var playable = ScriptPlayable<LoopBehaviour>.Create(graph);
        var clone = playable.GetBehaviour();

        clone.loop = loop;

        return playable;
    }
}
using UnityEngine.Timeline;

[TrackClipType(typeof(LoopClip))]
public class LoopTrack : TrackAsset {}

つぎに開始時間を代入するやり方
PlayableBehaviourのclipへTrackAssetで取得したclipをPlayableAssetを経由して代入している

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

public class LoopBehaviour : PlayableBehaviour
{
    public bool loop = true;

    public GameObject trackBinding;
    public TimelineClip clip;

    public override void OnBehaviourPause(Playable playable, FrameData info)
    {
        if (!loop) return;
        
        if(playable.GetGraph().GetResolver() is PlayableDirector director)
            director.time = clip.start;
    }
}
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

public class LoopClip : PlayableAsset, ITimelineClipAsset
{
    public bool loop = true;
    [HideInInspector]
    public TimelineClip clip;

    ClipCaps ITimelineClipAsset.clipCaps { get; }

    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        var playable = ScriptPlayable<LoopBehaviour>.Create(graph);
        var clone = playable.GetBehaviour();

        clone.loop = loop;
        clone.clip = clip;

        return playable;
    }
}
using UnityEngine.Timeline;

[TrackClipType(typeof(LoopClip))]
public class LoopTrack : TrackAsset
{
    protected override void OnCreateClip(TimelineClip clip)
    {
        var loopClip = clip.asset as LoopClip;
        loopClip.clip = clip;
    }
}

TrackAssetでは以下のようにしてClipを取得することもできる

    protected override Playable CreatePlayable(PlayableGraph graph, GameObject gameObject, TimelineClip clip)
    {
        foreach (var clip in GetClips())
        {
            var loopClip = clip.asset as LoopClip;
            loopClip.clip = clip;
        }
        return base.CreatePlayable(graph, gameObject, clip);
    }
    public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
    {
        foreach (var clip in GetClips())
        {
            var loopClip = clip.asset as LoopClip;
            loopClip.clip = clip;
        }
        return base.CreateTrackMixer(graph, go, inputCount);
    }
2
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
2
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?