Search Unity

How to do something in my PlayableBehaviour when I reach the end of the clip?

Discussion in 'Timeline' started by jeango, Sep 24, 2020.

  1. jeango

    jeango

    Joined:
    Dec 19, 2012
    Posts:
    109
    So I would like to make a simple thing: Display a text with a clip, if the mouse is clicked, I want to skip to the end of the clip, and when I reach the end of the clip, if the mouse wasn't clicked yet, I pause the director until the click finally happens.

    Here's what I have thus far. When the behaviour starts playing, I start a coroutine that waits for the click and skips to the end when it happens.
    now I would like to find a way to check at the end of the clip if the mouse was pressed, and if it wasn't, pause (until it's finally pressed).

    There's unfortunately no such thing as an "OnBehaviourEnd" callback, so I have no idea how to do this.
    Pretty sure checking for the current time in ProcessFrame is a bad idea because it will probably never be called exactly at the end of the clip.

    Code (CSharp):
    1.    public class WaitForClickBehaviour : PlayableBehaviour
    2.     {
    3.         public PlayableDirector director;
    4.         private bool _wasCLicked = false;
    5.         private bool _skipped = false;
    6.         private PlayableGraph graph;
    7.         private Playable thisPlayable;
    8.  
    9.         public override void OnPlayableCreate(Playable playable)      
    10.         {
    11.             graph = playable.GetGraph();  
    12.             thisPlayable = playable;
    13.         }
    14.        
    15.         public override void OnBehaviourPlay(Playable playable, FrameData info)
    16.         {
    17.             GameManager.Instance.StartCoroutine(WaitForClick());
    18.         }
    19.  
    20.         public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    21.         {
    22.            
    23.         }
    24.  
    25.         private IEnumerator WaitForClick()
    26.         {
    27.             while (Input.GetMouseButtonDown(0) == false)
    28.             {
    29.                 yield return null;
    30.             }
    31.             _wasCLicked = true;
    32.             JumpToEndofPlayable();
    33.         }
    34.  
    35.         private void JumpToEndofPlayable()
    36.         {
    37.             graph.GetRootPlayable(0).SetTime(graph.GetRootPlayable(0).GetTime() + thisPlayable.GetDuration());
    38.         }
    39.     }
     
  2. cdr9042

    cdr9042

    Joined:
    Apr 22, 2018
    Posts:
    173
    You can add a signal or a custom marker at the end of the timeline to listen to the event.
    25games has amazing tutorials about this


    Signal is simpler to use. You create signal asset, add a signal listener component, set reference to the signal asset and the function you want to call. Then add the signal to the timeline
    For the marker, you'll have to create a marker script, a listener that implements INotificationReceiver
     
  3. jeango

    jeango

    Joined:
    Dec 19, 2012
    Posts:
    109
    @cdr9042 oooooh I knew about signals and how to use them, but they were lacking the flexibility I needed.

    Markers are definitely extremely appealing here thanks for the tip
     
  4. KrabbyQ

    KrabbyQ

    Joined:
    Dec 19, 2016
    Posts:
    5
    Very late to the game but I found the answer in this post