• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by EricED · May 31, 2018 at 05:27 PM · animationtimelinerecordingclips

Recording animation into the timeline (and playing it back) at runtime

I've been working on this little project for 2 weeks now, and I've run in to 2 questions that are really blocking me.

  1. How do I get the timeline to accept new animations at runtime?

  2. How do I get the timeline to update when I create new tracks


I'm attempting to record animation from runtime directly into the Unity timeline. I have this prefab that I instantiate (the movement of this prefab is what will be recorded into animation clips in timeline tracks, and in it's Start() function, I have it create a new track with animation clip in the timeline.

Using the GameObjectRecorder, I'm able to record animation directly into these clips. But when I stop the recording and scrub back in the timeline (during runtime), it won't play back this new animation. If I have the prefab sitting in the scene before runtime, and it's assigned to an animation track, and I attach a premade .anim Asset to that track, it will work fine in runtime, I can scrub back and forth over the animation. But not when the animation is created and added DURING runtime.

Is it impossible for the timeline to accept new clips and play them back in runtime?

Here is my code that creates the animation tracks, and records the animation into them:

     public bool record = false;
     public AnimationClip clip;
 
     private GameObjectRecorder m_Recorder;
     private GameObject animNodeTransform;
     private SceneTransport sceneTransport;
     private PlayableDirector scenePlayableDirectorComponent;
 
     void Start()
     {
         sceneTransport = GameObject.Find("Timeline").GetComponent<SceneTransport>();
         animNodeTransform = gameObject.transform.Find("AnimNodeTransform").gameObject;
         m_Recorder = new GameObjectRecorder(gameObject);
         scenePlayableDirectorComponent = GameObject.Find("Timeline").GetComponent<PlayableDirector>();
 
         m_Recorder.BindComponent<Transform>(gameObject, false);
         m_Recorder.BindComponent<Transform>(animNodeTransform, false);
 
         //This is to create an Animation Clip
         clip = new AnimationClip
         {
             name = gameObject.name + " clip"
         };
 
         //instantiate playable for animationClip
         AnimationClipPlayable playable = AnimationClipPlayable.Create(scenePlayableDirectorComponent.playableGraph, clip);
 
         //get timelineAsset from playableDirector
         TimelineAsset timelineAsset = (TimelineAsset)scenePlayableDirectorComponent.playableAsset;
 
         //create new animationTrack on timeline
         var newTrack = timelineAsset.CreateTrack<AnimationTrack>(null, gameObject.name);
 
         //bind object to which the animation shall be assigned to the created animationTrack
         scenePlayableDirectorComponent.SetGenericBinding(newTrack, gameObject);
 
         //create a timelineClip for the animationClip on the AnimationTrack
         var timelineClip = newTrack.CreateClip(clip);
         timelineClip.displayName = gameObject.name + " clip";
     }
 
     void Update()
     {
         if (sceneTransport.playing && sceneTransport.recording)
         {
             record = true;
         }
         else
         {
             record = false;
         }
     }
 
     void LateUpdate()
     {
         if (record)
         {
             m_Recorder.TakeSnapshot(Time.deltaTime);
         }
         else if (m_Recorder.isRecording)
         {
             m_Recorder.SaveToClip(clip);
             m_Recorder.ResetRecording();
         }
     }

Also, a smaller problem, is that when I instantiate my gameObjects, they create tracks in the timeline, but I don't see them until I mess around with the editor in some way, like create a track group, then a bunch of animation tracks will just appear. Or if I close the Timeline window and open it again. How do I get the editor to update when I create the tracks?

Thanks to this thread over here for writing most of this code :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by seant_unity · Jun 01, 2018 at 11:37 AM

You may need to call this on your animation clips - https://docs.unity3d.com/ScriptReference/AnimationUtility.SetGenerateMotionCurves.html to get them to work in Timeline.

Also, if you change the timeline (e.g. add a new track or clip), via script you will need to call PlayableDirector.RebuildGraph() for the changes to take effect.

To get the editor to update, there is no script call yet, but you should be able to select a different object, then select the original playable director as a workaround to get timeline to rebuild the view. That should work unless the timeline window is locked.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EricED · Jun 01, 2018 at 11:13 PM 0
Share

Thanks! I'll test this out now.

avatar image EricED · Jun 01, 2018 at 11:56 PM 0
Share

Holy @#$% it worked!

avatar image jipsen EricED · Mar 05, 2020 at 05:31 PM 0
Share

Can you please share your code? I'm working on the same issue.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

284 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Recording animation (and playing it back) after build 0 Answers

timeline moves object with clip offset 1 Answer

Prevent Animator proceeding from within StateMachineBehavior without Parameter? 0 Answers

Record and replay controller movement 0 Answers

Recording animation doesn't work 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges