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 linanna · Aug 02, 2019 at 10:08 PM · animationcliptimeline

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

Hi, I am new to the timeline API. I need to create an animation track for quite a few of my prefabs on the timeline. Therefore, I am trying to write a script to automate the process. For each of the prefab, I want to write a script to create an animation track and place into the timeline, associate it with the animation clip and then adjust the position and rotation offset accordingly. I have gone into a few problems as below and would appreciate any help please.

1) I manage to create a new track and a default clip. The track is bind with the game object I want. However, it only works when the code is running. Once the code stops running, the originally binding for the track returns back to "None". Sometimes, I will get error about the Playable Graph is null.

2) The default clip is created but the animation clip has not been successfully bind to it.

3) I need to adjust the offset position and rotation of the animation clip and I cannot find the API to get access to the animation clip for each of the prefab and change it.

I would appreciate any help please. Thanks!

Here is the code for your reference:

     for (int i = 0; i < myPrefabs.Length; i++) {
         anim = myPrefabs[i].GetComponent<Animator>();

         if (anim != null)
         {
             AnimationClipPlayable playable = AnimationClipPlayable.Create
                 (playableDirector.playableGraph, myClip);

             if (playableDirector.playableGraph.IsValid())
             {
                 //Create output for animator to assign to
                 AnimationPlayableOutput output = AnimationPlayableOutput.Create
                     (playableDirector.playableGraph, myPrefabs[i].name + " output", anim);

                 //Set Source Playable to output
                 output.SetSourcePlayable<AnimationPlayableOutput, AnimationClipPlayable>(playable);
             } else
             {
                 Debug.Log("playable Graph is null, director hasn't started playing");
             }
         }

         //get Timeline from playableDirector
         TimelineAsset timelineAsset = (TimelineAsset)playableDirector.playableAsset;

         //Create New Animation Track on timeline
         newTrack = timelineAsset.CreateTrack<AnimationTrack>(null, myPrefabs[i].name + " Track");
         playableDirector.SetGenericBinding(newTrack, myPrefabs[i]);
         timelineClip = newTrack.CreateDefaultClip();
         var animationOutput = (AnimationPlayableOutput)playableDirector.playableGraph.GetOutput(0);
         animationOutput.SetTarget(anim);

     }
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
0
Best Answer

Answer by seant_unity · Aug 05, 2019 at 11:27 AM

Here's an example that should help. It looks like source of the confusion is between the timeline API and the playable API. In the example below it only uses the timeline API to construct everything, and the playable director to do the binding.

 // Create the track
 var newTrack = timelineAsset.CreateTrack<AnimationTrack>(null, name);
 
 // Create the clip and return the playable asset attached
 var animPlayableAsset = (AnimationPlayableAsset) newTrack.CreateClip(myClip).asset;
 
 // Set the clip offsets
 animPlayableAsset.position = myPosition;
 animPlayableAsset.rotation = myRotation; 
 
 // bind the track to the animator
 playableDirector.SetGenericBinding(newTrack, anim);
 
Comment
Add comment · Show 5 · 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 linanna · Aug 05, 2019 at 05:48 PM 0
Share

Hi, Thanks so much! You are my savior. I have been struggling on this and tried for almost a week and in vain. Thanks!!

This works! It has successfully created the track, clip and bind correctly. However, it does not appear on the timeline until I play.

I put the create code script as a custom button on the inspector of the timeline. Then, I drag the game Object, the clip and then press the custom button. In the console, I checked and the code has been run. But the created track with bind clip does not appear on the timeline. Once I run the game( hit play), then it appears.

Would you $$anonymous$$d letting me know what I may have missed please? Thanks again.

avatar image seant_unity linanna · Aug 05, 2019 at 06:49 PM 0
Share

Hmm...there is one thing to check. In an editor script, after you create the timeline asset and before you create any tracks, you need to save the timeline using AssetDatabase.CreateAsset(). Otherwise the tracks won't actually save to the timeline. $$anonymous$$aybe that's it?

avatar image linanna seant_unity · Aug 06, 2019 at 06:03 PM 0
Share

Hi, Thanks again for your great help and reply again! Sorry may be I did not explain clearly. The track and clip are actually created and are already saved on the timeline. The question, is for some reason, I need to hit "play" until I see them appear on the timeline. That was my question. Thanks!

Show more comments

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

111 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

Related Questions

Can't fully edit recorded animation after conversion to clip track. 0 Answers

Why the Animation Clip in Timeline is disabled gray I can't change animation clip ? 1 Answer

Timeline and Ragdoll 1 Answer

Timeline and AnimationClip problem -1 Answers

PlayableDirector.Pause() weird behavior 1 Answer


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