Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Changing Duration of custom playable clip

Discussion in 'Timeline' started by hoperin, Aug 15, 2018.

  1. hoperin

    hoperin

    Joined:
    Feb 9, 2014
    Posts:
    52
    I'm trying to make one of my custom playables become a specific duration at the beginning of it's life when i'ts added to the timeline. Adding code in Awake to the customPlayableClip class (here Anim_SpineAnimationClip.cs) works nicely -- it get my little "Hi!" whenever I ad a new clip to the timeline.
    But I havent figured out how to changed the duration of the clip itself.

    simply doing
    this.duration = 3f
    gives error CS0200: Property or indexer `UnityEngine.Playables.PlayableAsset.duration' cannot be assigned to (it is read-only)

    and trying
    TimelineClip thisClip = this as TimelineClip;
    thisClip.duration = 3f;
    gives error: Cannot convert type `ANIM_SpineAnimationClip' to `UnityEngine.Timeline.TimelineClip' via a built-in conversion

    Any idea how I can accomplish this?


    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5. using UnityEngine.Timeline;
    6.  
    7. [Serializable]
    8. public class ANIM_SpineAnimationClip : PlayableAsset, ITimelineClipAsset
    9. {
    10.     public ANIM_SpineAnimationBehaviour template = new ANIM_SpineAnimationBehaviour ();
    11.     // public ExposedReference<GameObject> animTarget;
    12.     public ExposedReference<Scr_SpineCtrl> animTarget;
    13.  
    14.  
    15.     public ClipCaps clipCaps
    16.     {
    17.         get { return ClipCaps.None; }
    18.     }
    19.  
    20.     public override Playable CreatePlayable (PlayableGraph graph, GameObject owner)
    21.     {
    22.         var playable = ScriptPlayable<ANIM_SpineAnimationBehaviour>.Create (graph, template);
    23.         ANIM_SpineAnimationBehaviour clone = playable.GetBehaviour ();
    24.         clone.animTarget = animTarget.Resolve (graph.GetResolver ());
    25.  
    26.         return playable;
    27.     }
    28.  
    29.     void Awake(){
    30.         Debug.Log("hi");  
    31.         this.duration = 3f;
    32.         //TimelineClip thisClip = this as TimelineClip;
    33.         //thisClip.duration = 3f;
    34.     }
    35.  
    36. }
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Override the duration property of PlayableAsset. That will let you set a default clip duration.
     
    pango likes this.
  3. hoperin

    hoperin

    Joined:
    Feb 9, 2014
    Posts:
    52
    hmm, I think I understand the basics of overriding as a concept but I can't get figure och how to implement this here--

    Code (CSharp):
    1. public override double duration = 3;
    gives error CS0106: The modifier `override' is not valid for this item

    and

    Code (CSharp):
    1.  
    2.     public override double duration {
    3.         get {
    4.             return base.duration;
    5.         }
    6.         set {
    7.             base.duration = value;
    8.         }
    9.     }
    while acknowledging duration as potentially overridable wont let me actually set anything:

    error CS0546: `ANIM_SpineAnimationClip.duration.set': cannot override because `UnityEngine.Playables.PlayableAsset.duration' does not have an overridable set accessor

    -- I must be going about this wrong?
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    public override double duration { get { return 3; } }. No set required.
     
  5. hoperin

    hoperin

    Joined:
    Feb 9, 2014
    Posts:
    52

    ahhhh that does it, thank you!!
     
  6. AdamBebkoSL

    AdamBebkoSL

    Joined:
    Dec 9, 2021
    Posts:
    33
    Thanks this is super helpful, although the solution is a bit obscure to know to do that, and it's not obvious from API docs