Search Unity

Access SpriteRenderer property: sprite in Timeline (using the Wizard)

Discussion in 'Timeline' started by pleasantPretzel, Aug 25, 2018.

  1. pleasantPretzel

    pleasantPretzel

    Joined:
    Jun 12, 2013
    Posts:
    29
    Hello :)

    I would like to create a custom Timeline track for the SpriteRenderer Component. My goal is to access the 'sprite' property. I would like to create clips in the Timeline track, and in those clips, be able to edit which sprite is being displayed by the SpriteRenderer.

    I've attached a screenshot below which shows the accessible properties of the SpriteRenderer binding. Why isn't the sprite property available here? I'm a bit of a novice so please bear with me if I'm misunderstanding something fundamental about sprites and/or the way Playables work.

    **My current solution is to use an animation track and to record my sprite edits in the inspector, but I would like try another route if possible.

    Thank you for your time!! :)

    Screen Shot 2018-08-25 at 5.58.15 PM.png
     
  2. JamesB

    JamesB

    Unity Technologies

    Joined:
    Feb 21, 2012
    Posts:
    112
    @slufter Hello there!

    From your screenshot I can see you have selected Standard Blend Playable. This a special type of timeline track/playable that the wizard can create that automatically writes all the code for you and blends the properties you give it. The drawback is that only some properties can be blended, floats, Vector3s, etc. If you wish to make a playable with the Sprite property you will need to make a normal track/playable by unselecting the Standard Blend Playable check box. This type of playable you will need to complete yourself as only the boilerplate is provided.

    I hope this helps.
     
  3. pleasantPretzel

    pleasantPretzel

    Joined:
    Jun 12, 2013
    Posts:
    29
    @JamesB Thank you, James! Yep, that helps, I now see why the Sprite property wasn't included in the Wizard.

    I'll make a custom playable and maybe hop back in here if I come across any major issue. Thanks again!
     
  4. pleasantPretzel

    pleasantPretzel

    Joined:
    Jun 12, 2013
    Posts:
    29
    @JamesB or anyone else who may be able to help,

    Aye! That took me days to figure out. I'm not so good at this. Either way, glad I figured something out. I'll leave my solution here for anyone who has time to review and suggest a better method, or for those who end up in here from search engines.

    Any thoughts on this?

    I figured no need to blend clips since I'm dealing with sprites, so I'm not using the mixer behavior script nor have I done anything in the track script.

    I used a Singleton Sprite Manager here because I couldn't figure out any other way to reference my SpriteRenderer directly from the behavior script. (Should I do that? I'd really like to do that to avoid extra scripts.) I don't know what is better practice, I'm still learning the basics (sorry, please bear with me!). If you have any recommendations, please share. Thank you!

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Playables;
    4.  
    5. [Serializable]
    6. public class SpriteChangerBehaviour : PlayableBehaviour
    7. {
    8.     public Sprite theSprite;
    9.  
    10.     private PlayableDirector director;
    11.  
    12.     public override void OnPlayableCreate(Playable playable)
    13.     {
    14.         director = (playable.GetGraph().GetResolver() as PlayableDirector);
    15.     }
    16.  
    17.     public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    18.     {
    19.         SpriteManagerTest.Instance.SetSprite(theSprite);
    20.     }
    21. }
     
    Last edited: Aug 30, 2018
  5. JamesB

    JamesB

    Unity Technologies

    Joined:
    Feb 21, 2012
    Posts:
    112
    Try something like this:

    Code (CSharp):
    1. [Serializable]
    2. public class SpriteTestBehaviour : PlayableBehaviour
    3. {
    4.     public Sprite sprite;
    5. }
    Code (CSharp):
    1. public class SpriteTestMixerBehaviour : PlayableBehaviour
    2. {
    3.     Sprite m_DefaultSprite;
    4.  
    5.     Sprite m_AssignedSprite;
    6.  
    7.     SpriteRenderer m_TrackBinding;
    8.  
    9.     public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    10.     {
    11.         m_TrackBinding = playerData as SpriteRenderer;
    12.  
    13.         if (m_TrackBinding == null)
    14.             return;
    15.  
    16.         if (m_TrackBinding.sprite != m_AssignedSprite)
    17.             m_DefaultSprite = m_TrackBinding.sprite;
    18.  
    19.         int inputCount = playable.GetInputCount ();
    20.  
    21.         float totalWeight = 0f;
    22.         float greatestWeight = 0f;
    23.         int currentInputs = 0;
    24.  
    25.         for (int i = 0; i < inputCount; i++)
    26.         {
    27.             float inputWeight = playable.GetInputWeight(i);
    28.             ScriptPlayable<SpriteTestBehaviour> inputPlayable = (ScriptPlayable<SpriteTestBehaviour>)playable.GetInput(i);
    29.             SpriteTestBehaviour input = inputPlayable.GetBehaviour ();
    30.          
    31.             totalWeight += inputWeight;
    32.  
    33.             if (inputWeight > greatestWeight)
    34.             {
    35.                 m_AssignedSprite = input.sprite;
    36.                 m_TrackBinding.sprite = m_AssignedSprite;
    37.                 greatestWeight = inputWeight;
    38.             }
    39.  
    40.             if (!Mathf.Approximately (inputWeight, 0f))
    41.                 currentInputs++;
    42.         }
    43.  
    44.         if (currentInputs != 1 && 1f - totalWeight > greatestWeight)
    45.         {
    46.             m_TrackBinding.sprite = m_DefaultSprite;
    47.         }
    48.     }
    49. }
    Totally untested (might not even compile!) but should be at least close enough to give you an idea. You need to include Track and Clip classes as well of course.
     
  6. pleasantPretzel

    pleasantPretzel

    Joined:
    Jun 12, 2013
    Posts:
    29
    @JamesB Thank you so much, James!! This works perfectly. I really appreciate you taking the time to help me out.

    Looking at your code, I realize in my previous attempts I had confused the default and assigned variables and was using them incorrectly throughout my mixer code. I see a little more clearly now, I think! Thank you again, you've been a HUGE help! :)
     
  7. ghtx1138

    ghtx1138

    Joined:
    Dec 11, 2017
    Posts:
    113
    I love you both :)
     
  8. system-idle

    system-idle

    Joined:
    Dec 13, 2013
    Posts:
    20
    Hi guys, thanks so much for this!

    I need to update Sprites on a GameObject with a SpriteRenderer as I scrub back and forth in the Timeline window, but I'm not sure how to implement the Scripts?

    I've created a GameObject called "Timeline" with PlayableDirector component.
    And a GameObject "Sprites" with SpriteRenderer component, and a Class that has a SpriteTestBehaviour property;

    I've added an animation track to Timeline GameObject, and dragged Sprites GameObject to it, but I'm not sure if that's correct way to implement?


    Thank you for your time!
     
  9. JamesB

    JamesB

    Unity Technologies

    Joined:
    Feb 21, 2012
    Posts:
    112
    @system-idle To just animate a sprite you shouldn't need to create any new playables using the Timeline Wizard. Create your sprite animation as you normally would, then drag it onto an animation track which references the animator that would normally animate the sprite renderer (one which is on the same or parent gameobject of the sprite renderer).
     
  10. system-idle

    system-idle

    Joined:
    Dec 13, 2013
    Posts:
    20
    Thanks JamesB for your reply.
    Right, gotcha. Drag an animation into an animation track. I was dragging the GameObject with SpriteRenderer onto an animation track.

    Is there a way to see video frames in Timeline? I'm using AvPro video player. Don't suppose you have any ideas?

    Thanks again for your time.
     
    Last edited: Aug 20, 2019
  11. system-idle

    system-idle

    Joined:
    Dec 13, 2013
    Posts:
    20
    Also, how/where do you open the Timeline Wizard dialog?
     
  12. JamesB

    JamesB

    Unity Technologies

    Joined:
    Feb 21, 2012
    Posts:
    112
    @system-idle The Timeline Playable Wizard is part of the Default Playables package on the Asset Store. You open it by going to the Window menu and selecting Timeline Playable Wizard...

    So far as I know there isn't a way of viewing the specific frames of a video in the timeline window though you can play them of course.