Search Unity

Access Timeline clip duration from within Clip script

Discussion in 'Timeline' started by Smidgens, Nov 10, 2018.

  1. Smidgens

    Smidgens

    Joined:
    Nov 2, 2014
    Posts:
    25
    I'm a bit stumped here. What I'm trying to do here is to access the following information from the clip itself:
    upload_2018-11-10_19-34-43.png

    There is a property called "duration" that you can access from a Clip script (PlayableAsset), but it doesn't return the duration as shown on the timeline. It always returns infinity, unless I decide to override it and return something fixed.

    The reason I'm asking is because of clip looping. I'm making my custom tracks repeat behaviours an n number of times within the duration of a clip. It's fairly easy to implement this the way I want it using the timeline mixer, but I also want the loops to show on the clip in the timeline. Which is why I need to make the clipcaps return Looping.

    In short: From the clip, I need to know exactly how long in seconds the clip on the timeline is, as shown in the inspector. If this isn't possible I'd be just as curious to know why.
     
  2. Bonfire-Boy

    Bonfire-Boy

    Joined:
    Nov 19, 2014
    Posts:
    5
    I had this problem when trying to get a custom Clip's duration from a custom Behaviour. I've got a reference to the Clip in the Behaviour (initialised in my Clip.CreatePlayable) and am able to access the fields that I've added to the Clip class from it, so was expecting to be able to use clip.duration in there too. But it returns Infinity.

    The solution turned out to be a lot simpler, you can just use playable.GetDuration().

    Code (CSharp):
    1.     [TrackClipType(typeof(CustomClip))]
    2.     public class CustomTrack : TrackAsset
    3.     {
    4.         public ScriptableObject trackData;
    5.  
    6.         public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
    7.         {
    8.             foreach (var c in GetClips())
    9.             {
    10.                 (c.asset as CustomClip).parentTrack = this;
    11.             }
    12.             return base.CreateTrackMixer(graph, go, inputCount);
    13.         }
    14.     }
    15.  
    16.     public class CustomClip: PlayableAsset, ITimelineClipAsset
    17.     {
    18.         public CustomTrack parentTrack;
    19.  
    20.         public CustomBehaviour template = new CustomBehaviour();
    21.  
    22.         public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    23.         {
    24.             var playable = ScriptPlayable<CustomBehaviour>.Create(graph, template);
    25.             (playable.GetBehaviour() as CustomBehaviour).clip = this;
    26.             return playable;
    27.         }
    28.     }
    29.  
    30.     public class CustomBehaviour : PlayableBehaviour
    31.     {
    32.         public CustomClip clip;
    33.  
    34.         public override void OnBehaviourPlay(Playable playable, FrameData info)
    35.         {
    36.             Debug.Log(clip.parentTrack.trackData.name); // finds this ok
    37.             Debug.Log(clip.duration); // prints Infinity
    38.             Debug.Log(playable.GetDuration()); // prints duration of clip
    39.         }
    40.  
    41.     }
    42.  
    43.  
     
    i_korsakov and INeatFreak like this.