Search Unity

Cannot get clip length for ControlTrack controlled MonoBehaviour (through ITimeControl)

Discussion in 'Timeline' started by sbstn-hn, Oct 25, 2017.

  1. sbstn-hn

    sbstn-hn

    Joined:
    May 23, 2013
    Posts:
    3
    Hi guys,

    timeline looks very nice so far but I am somewhat lost when it comes to scripting it.
    Scenario: I have a MonoBehaviour that is controlled via ITimeControl and a Control Track. The "SetTime(double time)" function gets called as expected but it only provides me with the absolute time inside my ControlPlayableAsset Clip.
    Is there any way to get some more information into my MonoBehaviour, like duration of the clip?

    I want to use the ControlTrack to blend between some values, so I would need either a normalized time or some way to normalize it, i.e. the duration of the clip.

    Any ideas?

    Thanks,
    Sebastian
     
  2. sbstn-hn

    sbstn-hn

    Joined:
    May 23, 2013
    Posts:
    3
    Ok,
    I figured out a possible solution:
    The key to success was to add an ExposedReference pointing to the target object into the PlayableAsset. See code below.

    Code (CSharp):
    1.  
    2.     [System.Serializable]
    3.     public class TimeControlAsset : PlayableAsset,  ITimelineClipAsset
    4.     {
    5.         public bool controlActivation;
    6.         public ExposedReference<GameObject> target;
    7.  
    8.         public override Playable CreatePlayable (PlayableGraph graph, GameObject go)
    9.         {
    10.             var playable = ScriptPlayable<TimeControlPlayableBehaviour>.Create (graph);
    11.             playable.GetBehaviour().Target = target.Resolve(graph.GetResolver());
    12.  
    13.             return playable;
    14.         }
    15.  
    16.         public ClipCaps clipCaps {
    17.             get {
    18.                 return ClipCaps.None;
    19.             }
    20.         }
    21.     }
    This reference is passed to the PlayableBehaviour script.

    Code (CSharp):
    1.   public class TimeControlPlayableBehaviour : PlayableBehaviour
    2.     {
    3.         [SerializeField] GameObject target;
    4.         public GameObject Target {
    5.             get {
    6.                 return target;
    7.             }
    8.             set {
    9.                 target = value;
    10.             }
    11.         }
    12.         public override void OnGraphStart(Playable playable)
    13.         {
    14.         }
    15.  
    16.         public override void OnBehaviourPlay(Playable playable, FrameData info)
    17.         {
    18.         }
    19.  
    20.         public override void OnBehaviourPause(Playable playable, FrameData info)
    21.         {
    22.         }
    23.  
    24.         public override void PrepareFrame(Playable playable, FrameData info)
    25.         {
    26.             if (target == null)
    27.                 return;
    28.  
    29.             var normalizedTime = playable.GetTime() / playable.GetDuration();
    30.             target.GetComponent<SomeType>.SetNormalizedTime(normalizedTime);
    31.             }
    32.         }
    33.     }
    I still have problems understanding the structure of the whole timeline system. It is very convenient to use but the documentation is quite poor (or i don't know where to look)

    Cheers, case closed.
    Sebastian
     
    Last edited: Oct 26, 2017