Search Unity

How to tell when a Clip on a Track is done?

Discussion in 'Timeline' started by Bunzaga, Dec 10, 2018.

  1. Bunzaga

    Bunzaga

    Joined:
    Jan 9, 2009
    Posts:
    202
    I'm messing with custom tracks and Playables, but ran into a snag, where I want to have a 'default' value, but then only if a Playable is there, it should use the Playables value.

    I've found a way around it, which I guess is ok, but it just feels very hacky, and I was hoping there was a native solution that I just missed in the API. Please review what I'm using, and let me know if there is a better way to accomplish the same thing :D TY!

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using UnityEngine;
    5. using UnityEngine.Playables;
    6.  
    7. public class MovementControlBehaviour : PlayableBehaviour
    8. {
    9.     public bool LockMovement;
    10.  
    11.     private Coroutine _destroy;
    12.     private int _lastFrame;
    13.     private Func<bool> _nextFrame = null;
    14.  
    15.     public override void OnPlayableCreate(Playable playable)
    16.     {
    17.         _lastFrame = Time.frameCount;
    18.         _nextFrame = () => { return _lastFrame + 1 <= Time.frameCount; };
    19.        
    20.         base.OnPlayableCreate(playable);
    21.     }
    22.  
    23.     public override void PrepareFrame(Playable playable, FrameData info)
    24.     {
    25.         _lastFrame = Time.frameCount;
    26.         base.PrepareFrame(playable, info);
    27.     }
    28.  
    29.     public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    30.     {
    31.         InputData inputData = playerData as InputData;
    32.         if (inputData != null)
    33.         {
    34.             inputData.MovementLocked = LockMovement;
    35.             if (_destroy == null)
    36.             {
    37.                 _destroy = inputData.StartCoroutine(OnClipDestroyed(inputData));
    38.             }
    39.         }
    40.     }
    41.  
    42.     public IEnumerator OnClipDestroyed(InputData inputData)
    43.     {
    44.         if (inputData == null)
    45.         {
    46.             yield break;
    47.         }
    48.  
    49.         while (inputData.MovementLocked)
    50.         {
    51.             yield return new WaitUntil(_nextFrame);
    52.             inputData.MovementLocked = false;
    53.         }
    54.         _destroy = null;
    55.     }
    56. }
    57.  
    58.