Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by rhaak17 · Sep 29, 2017 at 05:41 PM · runtimeanimationcliptimelineassign

Adding animation for object to timeline at runtime

Hello everybody,

I am currently playing around with the new timeline feature in unity. What I am trying to do is to assign an AnimationClip(I recorded at runtime) to an object of the scene AND add it to the timeline. Assigning the AnimationClip to the object through this code

 PlayableDirector scenePlayableDirectorComponent;
 AnimationClip clip;
 Animator animatorOfSelected;
 GameObject toAssignAnimationTo;
     
 //get Animator of object to assign animationClip to
 if (toAssignAnimationTo.GetComponent<Animator> () != null) {    
         animatorOfSelected = toAssignAnimationTo.GetComponent<Animator> ();
 } else {
         animatorOfSelected = toAssignAnimationTo.AddComponent<Animator> ();
 }
 
 //instantiate playable for animationClip
 AnimationClipPlayable playable = AnimationClipPlayable.Create(scenePlayableDirectorComponent.playableGraph, clip);
 
 //Create output for animator of object to assign animationClip to
 AnimationPlayableOutput output = AnimationPlayableOutput.Create(scenePlayableDirectorComponent.playableGraph, toAssignAnimationTo.name + " output", animatorOfSelected);
 
 //setSourcePlayable for output
 output.SetSourcePlayable<AnimationPlayableOutput, AnimationClipPlayable> (playable);

works fine.

But I want the animation to appear in the timeline I created in the editor.

My approach is this:

 GameObject toAssignAnimationTo;
 PlayableDirector scenePlayableDirectorComponent;
 
 //get timelineAsset from playableDirector
 TimelineAsset timelineAsset = (TimelineAsset)scenePlayableDirectorComponent.playableAsset;
 
 //create new animationTrack on timeline
 var newTrack = timelineAsset.CreateTrack<AnimationTrack> (null, "Animation Track " + toAssignAnimationTo.name);
 
 //bind object to which the animation shell be assigned to the created animationTrack
 scenePlayableDirectorComponent.SetGenericBinding (newTrack, toAssignAnimationTo);
 
 //create a timelineClip for the animationClip on the AnimationTrack
 var timelineClip = newTrack.CreateClip (clip);

which seems to go into the right direction:

timeline

But the animation on "Cube" is not doing anything(the animations for "redCube" and "blueCube" were recorded in the editor, they work fine).

I hope somebody give me some advise, Thanks in advance!

timeline.jpg (46.3 kB)
Comment
Add comment · Show 1
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 matasoy · Dec 08, 2019 at 11:04 AM 0
Share

I couldnt add any track so far now, but your codes work. Did you get any solution? I am strugling with these problems.

Thank you for your codes.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by seant_unity · Sep 29, 2017 at 06:26 PM

Hi,

You will need to generate motion curves for the object to use it in Timeline. There is a button to do that in the AnimationClip inspector.

Unfortunately there is no officially supported public API call to do that, but if you are so inclined, you can use reflection to access the static internal method AnimationUtility.SetGenerateMotionCurves (AnimationClip clip, bool value), until a public API is available.

Cheers

Comment
Add comment · Show 1 · 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 rhaak17 · Sep 29, 2017 at 07:34 PM 0
Share

Ah okay.. So at the moment there's no 'official' way to record an animation for an object at runtime so that this animation appears on the timeline? Or do I only have the wrong approach?

Edit: I just checked how to use the AnimationUtility.SetGenerate$$anonymous$$otionCurves (AnimationClip clip, bool value) method with reflection, but I'm not sure how exactly to do it, with reflection I only get access to the functions I could call the 'normal functioncall-way' too. Sorry if it is a stupid question but reflection is new to me..

Thanks!

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.

Follow this Question

Answers Answers and Comments

72 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

Related Questions

Animation clip on timeline. Character moves differently in Game and in animation preview. In game character accumulates a slip while walking 0 Answers

Remove object assignment in runtime? 1 Answer

Very, very strange float assignment problem. 2 Answers

Binding animation clip to Timeline and change position/rotation offsets of animation clip by API 1 Answer

How can I assign a GameObject to a variable at runtime? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges