Search Unity

Deactivate Track won't return state

Discussion in 'Timeline' started by fwalker, Jan 27, 2020.

  1. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    I created a DeactivateBehaviour for Timeline after some trials and tribulations. It follows the standard pattern and then I referenced the process used on the Default TextSwitcherMixerBehaviour playable. So the Mixer looks like this:

    Code (CSharp):
    1.  
    2. public class DeactivateMixerBehaviour : PlayableBehaviour
    3. {
    4.     GameObject trackBindingObj;
    5.     bool isActive;
    6.     bool firstFrameHappened;
    7.  
    8.     // NOTE: This function is called at runtime and edit time.  Keep that in mind when setting the values of properties.
    9.     public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    10.     {
    11.         trackBindingObj = playerData as GameObject;
    12.  
    13.         if (!trackBindingObj)
    14.             return;
    15.      
    16.         if (!firstFrameHappened)
    17.             {
    18.             isActive = trackBindingObj.activeInHierarchy;
    19.             firstFrameHappened = true;
    20.             }
    21.  
    22.  
    23.         int inputCount = playable.GetInputCount ();
    24.  
    25.         for (int i = 0; i < inputCount; i++)
    26.         {
    27.             float inputWeight = playable.GetInputWeight(i);
    28.             ScriptPlayable<DeactivateBehaviour> inputPlayable = (ScriptPlayable<DeactivateBehaviour>)playable.GetInput(i);
    29.             DeactivateBehaviour input = inputPlayable.GetBehaviour ();
    30.  
    31.             if (inputWeight > 0)
    32.                 trackBindingObj.SetActive(false);
    33.  
    34.             }
    35.     }
    36.     public override void OnPlayableDestroy(Playable playable)
    37.         {
    38.         firstFrameHappened = false;
    39.  
    40.         if (trackBindingObj == null)
    41.             return;
    42.  
    43.         trackBindingObj.SetActive(isActive);
    44.         }
    45.     }
    46.  

    This works great to set the gameObject inactive but when scrubbing the timeLine in the editor, the object never returns to its active state. What am I missing?
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    OnPlayableDestroy is only called when the graph is destroyed. Which can happen when the whole timeline is completed.

    To turn on the clip when there is no clip present, you will need to do that inside of ProcessFrame. Look at ActivationMixerPlayable inside the Timeline Package as an example - it's nearly identical.
     
  3. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    Thank you, Sean,
    It did not even occur to me that the Activate track code might be available! :) ...

    And for anyone wondering. Really, it was pretty simple! :( (I was sleepy, that's my excuse and I am sticking to it! )

    When inputWeight is not greater than 0 then we want the object to be inactive so like this:

    Code (CSharp):
    1. public class DeactivateMixerBehaviour : PlayableBehaviour
    2.  
    3.         int inputCount = playable.GetInputCount();
    4.         bool hasInput = true;
    5.         for (int i = 0; i < inputCount; i++)
    6.             {
    7.             if (playable.GetInputWeight(i) > 0)
    8.                 {
    9.                 hasInput = false;
    10.                 break;
    11.                 }
    12.             }
    13.  
    14.         trackBindingObj.SetActive(hasInput);
    15.  
    16.