Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Permanent state change after preview

Discussion in 'Timeline' started by TheAnosmic, Aug 3, 2019.

  1. TheAnosmic

    TheAnosmic

    Joined:
    Sep 9, 2015
    Posts:
    4
    I'm trying to create a track that changes the sprite when it's finished, but every time I watch its preview it's permanently changing the sprite renderer sprite.
    I've trying to use GatherProperties for that but it didn't work.
    (it's also disabling the animator, which also doesn't revert after preview).
    As a workaround (it's messing my scenes all the time) I've added "EditorApplication.isPlaying" guard.

    Code (CSharp):
    1.     [TrackBindingType(typeof(SpriteRenderer))]
    2.     [TrackClipType(typeof(ForceEndOfAnimation))]
    3.     public class ForceEndOfAnimationTrack : TrackAsset
    4.     {
    5.         public override void GatherProperties(PlayableDirector director, IPropertyCollector driver)
    6.         {
    7.             var binding = director.GetGenericBinding(this);
    8.             var spriteRenderer = binding as SpriteRenderer;
    9.             if (spriteRenderer == null)
    10.             {
    11.                 base.GatherProperties(director, driver);
    12.                 return;
    13.             }
    14.             driver.AddFromComponent(spriteRenderer.gameObject, spriteRenderer);
    15.             driver.AddFromComponent(spriteRenderer.gameObject, spriteRenderer.gameObject.GetComponent<Animator>());
    16.             base.GatherProperties(director, driver);
    17.         }
    18.     }
    Code (CSharp):
    1.     public class ForceEndOfAnimation : PlayableAsset
    2.     {
    3.         [SerializeField] private ForceEndOfAnimationPlayable template;
    4.  
    5.         public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    6.         {
    7.             return ScriptPlayable<ForceEndOfAnimationPlayable>.Create(graph, template);
    8.         }
    9.     }
    Code (CSharp):
    1.     [Serializable]
    2.     public class ForceEndOfAnimationPlayable : PlayableBehaviour
    3.     {
    4.  
    5.         private SpriteRenderer spriteRenderer;
    6.         public Sprite endSprite;
    7.         private Animator animator;
    8.  
    9.         public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    10.         {
    11.             if (spriteRenderer == null)
    12.             {
    13.                 spriteRenderer = (SpriteRenderer) playerData;
    14.                 animator = spriteRenderer.GetComponent<Animator>();
    15.             }
    16.  
    17.         }
    18.  
    19.         public override void OnBehaviourPause(Playable playable, FrameData info)
    20.         {      
    21.             if (!EditorApplication.isPlaying)
    22.             {
    23.                 base.OnBehaviourPause(playable, info);
    24.                 return;
    25.             }
    26.             if (animator != null)
    27.                 animator.enabled = false;
    28.             if (spriteRenderer != null)
    29.                 spriteRenderer.sprite = endSprite;
    30.             base.OnBehaviourPause(playable, info);
    31.         }
    32.     }
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,278
    It looks like AddFromComponent isn't properly detecting the sprite field.

    Try this to put the sprite field into preview explicitly, along with the enable field on the animator.

    Code (CSharp):
    1. [TrackClipType(typeof(SpriteClip))]
    2. [TrackBindingType(typeof(SpriteRenderer))]
    3. public class SpriteTrack : TrackAsset
    4. {
    5.     public override void GatherProperties(PlayableDirector director, IPropertyCollector driver)
    6.     {
    7.         var binding = director.GetGenericBinding(this);
    8.         var spriteRenderer = binding as SpriteRenderer;
    9.         if (spriteRenderer != null)
    10.         {
    11.             driver.AddFromName<SpriteRenderer>(spriteRenderer.gameObject, "m_Sprite");
    12.             driver.AddFromName<Animator>(spriteRenderer.gameObject, "m_Enabled");
    13.         }          
    14.     }
    15. }
    16.  
     
unityunity