Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

How can I get the CLIP Duration during Runtime

Discussion in 'Timeline' started by sabther, Dec 22, 2018.

  1. sabther

    sabther

    Joined:
    Sep 5, 2018
    Posts:
    20
    Hello There

    I am trying to customize a feature/Function for my project when using timeline/Cinemachine

    I what my script to read the duration of each clip then pass it to a function as parameter



    As you can see in the picture, I am trying to get the Duration as indicate image above.

    I have managed to get the Start time/Frame but not duration currently.

    Don't know if this is the right place to get some answer

    Thanks

    WZ
     
  2. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    589
    I just did it my self today.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Playables;
    6. using UnityEngine.Timeline;
    7.  
    8. public class MyTimeline : MonoBehaviour
    9. {
    10.     public TimelineAsset timelineasset;
    11.     public List<AnimationTrack> animationTracks = new List<AnimationTrack>();
    12.  
    13.     private void Start()
    14.     {
    15.         var rootTracks = timelineasset.GetRootTracks();
    16.  
    17.         foreach(var track in rootTracks)
    18.         {
    19.             if (track.name.Contains("Animation"))
    20.             {
    21.                 animationTracks.Add((AnimationTrack)track);
    22.             }
    23.         }
    24.  
    25.         animationTracks[0].trackOffset = TrackOffset.ApplyTransformOffsets;
    26.         var c = animationTracks[0].GetClips();
    27.         foreach(var clip in c)
    28.         {
    29.          
    30.         }
    31.     }
    32. }
    33.  
    Now when you type clip. inside the last bottom foreach you can get all the properties like:
    clip.duration or clip.end or clip.start and so on.



    Just remember to assign in the timelineasset
     
  3. sabther

    sabther

    Joined:
    Sep 5, 2018
    Posts:
    20
    HI Chocolade

    Can you explain a little about the last part

    animationTracks[0].trackOffset = TrackOffset.ApplyTransformOffsets;
    var c = animationTracks[0].GetClips();
    foreach(var clip in c)
    {

    }

    so you only did offset and get clips from the very first Track?
    But animation tracks does contain multiple tracks?

    Thank you
     
    Chocolade likes this.
  4. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    589
    In my case I have 3 animation tracks in the timeline but I'm testing and using only the first one animationTracks[0]
    Then I'm getting from it the clips there are and getting the info like you wanted.

    Ah...I'm getting from the first track for my tests and indeed animationTracks contain multiple animation tracks.
    It's containing all the animation tracks you add/added to the timeline in the editor.
    In my case there 3 tracks in the animationTracks.

    You can loop now over the animationTracks and get the information from each track:

    Code (csharp):
    1.  
    2. if (animationTracks.Count > 0)
    3.         {
    4.             for (int i = 0; i < animationTracks.Count; i++)
    5.             {
    6.                 //animationTracks[i].trackOffset = TrackOffset.ApplyTransformOffsets; This line is for                    my use just for testing.
    7.  
    8.                 var c = animationTracks[i].GetClips();
    9.                 foreach (var clip in c)
    10.                 {
    11.  
    12.                 }
    13.             }
    14.         }
    15.