Search Unity

Adding animation keyframes to a infinite clip from a custom editor

Discussion in 'Timeline' started by panta, Jul 27, 2018.

  1. panta

    panta

    Joined:
    Aug 10, 2012
    Posts:
    71
    Hi @seant_unity and other Timeline devs,

    Our team's designer really wants to be able to author animation events for gameplay triggers on timeline. I'm aware that Timeline doesn't support adding events directly on the timeline, such as was shown at Unite 2017:
    upload_2018-7-26_21-59-3.png
    I'd love to have this feature, but as a workaround, I want to use normal AnimationEvents stored on an AnimationClip, since it seems the bug where AnimationEvents weren't triggering from Timeline is fixed in Unity 2018.2. Our designer doesn't like having to open up the Animation window from Timeline to edit the AnimationEvents, so I want to find a way to create the AnimationEvents directly from Timeline and show some visualization on the AnimationClip.

    To make it easier to display the AnimationEvents directly on the timeline, I want to create a custom track with a custom editor that has a reference to an AnimationTrack that is set to "Infinite" mode. This custom track (let's call it GameplayEventsTrack) has a reference to the AnimationTrack, and has buttons in it's custom editor to add and remove AnimationEvents on the AnimationTrack's AnimationClip at the current playhead position. At the same timestamp as the AnimationEvent, I want to put a placeholder keyframe so that when I press the "Add AnimationEvent" button in the custom editor, I get a result in the timeline like this:
    upload_2018-7-26_22-14-10.png

    The idea being that the designer can (without leaving the Timeline) add AnimationEvents with a single button press, and the placeholder keyframe shows a visual on the Timeline (since events are not visualized).

    So far, I've done some experimenting and I've gotten to the point where I can add and remove animation events and keyframes on the AnimationTrack from my GameplayEventsTrack, but I've run into 2 roadblock:

    1.) is there a good way in the Editor to get the current time of the playhead while you're in preview mode?
    upload_2018-7-26_22-14-29.png

    2.) I was able to access the infinite AnimationClip in the AnimationTrack via reflection (I'm using a Reflection function that allows me to access the private AnimationClip m_AnimClip), but when I add new keyframes and AnimationEvents to this AnimationClip, I can't get the Timeline to refresh to show the keyframes. The only way they show up is if I either recompile some new code, or open the Animation window and add a new keyframe or animation event. I know that the keyframes are writing to the AnimationTrack, because if I expand the graph inline on the Timeline, I can see the keys I added, but not the tick marks on the track:
    upload_2018-7-26_22-30-10.png

    Is there some function I can call to force the visuals for the AnimationTrack to update after I add the new event? Is there a way I can call GatherProperties or some other function to update the AnimationTrack?

    Thank you!
     

    Attached Files:

    Last edited: Jul 27, 2018
  2. panta

    panta

    Joined:
    Aug 10, 2012
    Posts:
    71
    @seant_unity
    I think this is perhaps the method I need to call?
    upload_2018-7-27_0-31-36.png
     
  3. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    You will need reflection, but I think what you need is TimelineWindow.m_State.Refresh(). That will reconstruct the view of the timeline window and should include the appropriate animation keys on the infinite track.

    In 2018.3, this will be exposed publicly as TimelineEditor.Refresh();

    And there is an event system coming for Timeline - looking like it will land in 2019.1.

    And to answer
    1.) is there a good way in the Editor to get the current time of the playhead while you're in preview mode?

    Yes, TimelineEditor.inspectorDirector.time will give you the time of the playhead.
     
    panta likes this.