Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

jumping to specific points on Timeline

Discussion in 'Timeline' started by Bentoon, Nov 3, 2020.

  1. Bentoon

    Bentoon

    Joined:
    Apr 26, 2013
    Posts:
    98
    I have searched and searched, but this still evades me...

    I have a master timeline and have set signals to pause at certain points where a player gets to make a choice and jump to different points in the timeline.

    I cannot figure out how to jump to specific points in the timeline itself... I thought this would be achievable with markers, but it seems more difficult than I thought...

    Any help is appreciated!

    ~ be
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    It requires some scripting, and depends on your timeline.

    You can get access to the markers from the timeline asset. (playableDirector.playableAsset as TimelineAsset). From there, you need to know which track your markers are on.

    If they are placed in the header area, you can get that using timelineAsset.markerTrack. Then the markers (signals, or other types of markers) can be accessed using trackAsset.GetMarkers(), and the time accessed from the iMarker.time property.

    For example, to set the playable director to the first marker on the marker track.

    Code (CSharp):
    1.          var timelineAsset = playableDirector.playableAsset as TimelineAsset;
    2.             var markers = timelineAsset.markerTrack.GetMarkers().ToArray();
    3.             playableDirector.Play();
    4.             playableDirector.time = markers[0].time;
     
  3. Bentoon

    Bentoon

    Joined:
    Apr 26, 2013
    Posts:
    98
    @seant_unity
    Thank you for the quick response Sean!

    I am trying to put this on a game Obj but getting errors :

    Assets/_Scripts/timelineMarker.cs(25,66): error CS1061: 'IEnumerable<IMarker>' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'IEnumerable<IMarker>' could be found (are you missing a using directive or an assembly reference?)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5. using UnityEngine.Timeline;
    6.  
    7.  
    8. public class timelineMarker : MonoBehaviour
    9. {
    10.  
    11.     private PlayableDirector playableDirector;
    12.     //public GameObject controlPanel;
    13.  
    14.  
    15.     public int markerNum;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         playableDirector = GetComponent<PlayableDirector>();
    20.     }
    21.  
    22.     void OnMouseUp()
    23.     {
    24.            var timelineAsset = playableDirector.playableAsset as TimelineAsset;
    25.             var markers = timelineAsset.markerTrack.GetMarkers().ToArray();
    26.             playableDirector.Play();
    27.             playableDirector.time = markers[markerNum].time;
    28.     }
    29. }

    Thanks!
    ~be


     
  4. Bentoon

    Bentoon

    Joined:
    Apr 26, 2013
    Posts:
    98
    @seant_unity
    Hello hello
    Thanks! What assembly am I missing? I keep getting this error in your script:

    Assets/_Scripts/timelineMarker.cs(25,66): error CS1061: 'IEnumerable<IMarker>' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'IEnumerable<IMarker>' could be found (are you missing a using directive or an assembly reference?)
     
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    using System.Linq.

    The ToArray() call is from the Linq library. It turns a IEnumerable into a C# array.
     
    Bentoon likes this.
  6. Bentoon

    Bentoon

    Joined:
    Apr 26, 2013
    Posts:
    98
    @seant_unity

    THANK YOU !

    For all those checking this threat out, This is a Great way to JUMP to different "Markers" / in a Master timeline...
    For example, you have a menu where you press buttons that will take you to different places on your timeline...
    (NOTE: the Marker # is not necessarily sequentially next marker ... Marker # may have to do with the order in which they are created ... )


    working code below, please mod & make more functional & post
    Thanks again @seant_unity
    ~be




    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.Playables;
    4. using UnityEngine.Timeline;
    5. using System.Linq;
    6.  
    7.  
    8.  
    9.  
    10. public class timelineMarker : MonoBehaviour
    11.  
    12. {
    13.     // This is to assign ANOTHER timeline (not just to one on this Game Object)
    14.     // b/c This game object is a trigger - someting you mouse up or gaze @ -
    15.     //  that will take you to a specific "Marker"
    16.     // NOTE: the Marker # is not necessarily sequential... Marker # may have to do with the order in which they are created
    17.     public PlayableDirector playableDirector;
    18.     // The # of the Marker you want to go to
    19.     public int markerNum;
    20.  
    21.     void Start()
    22.     {
    23.         // THIS WOULD GRAB THE TIMELINE ON THIS OBJECT but I rather call a MASTER timeline!
    24.        // playableDirector = GetComponent<PlayableDirector>();
    25.     }
    26.  
    27.     void OnMouseUp()
    28.     {
    29.            
    30.             var timelineAsset = playableDirector.playableAsset as TimelineAsset;
    31.             var markers = timelineAsset.markerTrack.GetMarkers().ToArray();
    32.             playableDirector.Play();
    33.             playableDirector.time = markers[markerNum].time;
    34.            
    35.     }
    36. }
    37.  
     
  7. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,455
    Is there any way to grab the markers in the order they appear on the marker track and not in the order they were created?
     
    brian-nielsen likes this.
  8. KhaiTick

    KhaiTick

    Joined:
    Jul 17, 2017
    Posts:
    1
    Can sort with time using System.Linq
    Code (CSharp):
    1. var markers = timelineAsset.markerTrack.GetMarkers().OrderBy((marker) => marker.time).ToArray();
     
    paulygons, brian-nielsen and ROBYER1 like this.
  9. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,455
    Oh hell yes
     
    brian-nielsen likes this.
  10. leeprobert

    leeprobert

    Joined:
    Feb 12, 2015
    Posts:
    50
    I have a custom 'AnnotationMarker' with a title property. Any idea how I can grab the marker with a title? I tried this:
    Code (CSharp):
    1. AnnotationMarker marker = timelineAsset.markerTrack.GetMarkers().First((AnnotationMarker marker) => marker.title.Contains('foo'));
    But it doesn't recognise 'First' on IEnumerator. I think it is the cast though.