Search Unity

Resolved How can I add an animation to a Timeline via script?

Discussion in 'Timeline' started by AmitChBB, Feb 23, 2021.

  1. AmitChBB

    AmitChBB

    Joined:
    Feb 16, 2021
    Posts:
    37
    Hi there, I've looked through the documentation and forum but was unable to find an answer to this question.

    Is it even possible to add animations to TimelineAssets via script?

    Ideally, I would want to write logic like this:

    Code (CSharp):
    1.     [SerializeField] private Animation animationToAdd;
    2.     [SerializeField] private Animator objectToAnimate;
    3.     [SerializeField] private PlayableDirector director;
    4.  
    5.     private void Awake()
    6.     {
    7.         // 1. Create new Animation Track in director's TimelineAsset
    8.  
    9.         // 2. Make the created animation track reference the animationToAdd
    10.  
    11.         // 3. Edit the director's TimelineInstance and configure the bindings to reference objectToAnimate
    12.  
    13.         // 4. Play the timeline
    14.         director.Play();
    15.     }
    However, I'm not even sure how to do step 1. I am unsure of how to reference the TimelineAsset, since PlayableDirector only has a property for PlayableAsset and PlayableGraph.
     
  2. grahamsaulnier

    grahamsaulnier

    Unity Technologies

    Joined:
    Apr 10, 2018
    Posts:
    18
    TimelineAsset inherits from PlayableAsset. If you cast the director's asset to that, you'll then be able to use TimelineAsset's api to create an animation track and clip.
     
    mandisaw likes this.
  3. AmitChBB

    AmitChBB

    Joined:
    Feb 16, 2021
    Posts:
    37
    Absolutely! Thank you very much. I was able to get it to work.
    For those wondering, this is the code I wrote:
    Code (CSharp):
    1.     [SerializeField] private AnimationClip animationToAdd;
    2.     [SerializeField] private Animator objectToAnimate;
    3.     [SerializeField] private PlayableDirector director;
    4.  
    5.     private void Awake()
    6.     {
    7.         // 1. Create new Animation Track in director's TimelineAsset
    8.         TimelineAsset asset = director.playableAsset as TimelineAsset;
    9.         // Note - we're deleting the track if it exists already, since we want to generate everything on the spot for this example
    10.         foreach (TrackAsset track in asset.GetOutputTracks())
    11.             if (track.name == generatedAnimationTrackName)
    12.                 asset.DeleteTrack(track);
    13.         AnimationTrack newTrack = asset.CreateTrack<AnimationTrack>("MyTrackName");
    14.  
    15.         // 2. Make the created animation track reference the animationToAdd
    16.         TimelineClip clip = newTrack.CreateClip(animationToAdd);
    17.         clip.start = 0.5f;
    18.         clip.timeScale = 2f;
    19.         clip.duration = clip.duration / clip.timeScale;
    20.  
    21.         // 3. Edit the director's TimelineInstance and configure the bindings to reference objectToAnimate
    22.         director.SetGenericBinding(newTrack, objectToAnimate);
    23.  
    24.         // 4. Play the timeline
    25.         director.Play();
    26.     }
     
    Ekumify and DeadCastles like this.
  4. grahamsaulnier

    grahamsaulnier

    Unity Technologies

    Joined:
    Apr 10, 2018
    Posts:
    18
    Just a note about your code.

    `GetOutputTracks()` returns an IEnumerable. Presently we simply return a cached array of the tracks. However, if this implementation changes in the future and we `yield return` the values to your one at a time, your `foreach` will throw an exception.
     
    DavidGeoffroy likes this.
  5. unity_74AA4DFB6823CFFBBF7F

    unity_74AA4DFB6823CFFBBF7F

    Joined:
    Oct 4, 2022
    Posts:
    1
    What libraries are imported to run animation in unity 2022 with script?. Is this code okay?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6.  
    7. public class SelecionarVentana : MonoBehaviour
    8. {
    9.     public GameObject VentanaGuerrero;
    10.     public GameObject VentanaGuitarra;
    11.     public GameObject VentanaPiano;
    12.     public GameObject VentanaTigre;
    13.  
    14.     Animation AVentanaGuerrero;
    15.     Animation AVentanaGuitarra;
    16.     Animation AVentanaPiano;
    17.     Animation AVentanaTigre;
    18.  
    19.     bool VisibleVGuerrero = false;
    20.     bool VisibleVGuitarra = false;
    21.     bool VisibleVPiano = false;
    22.     bool VisibleVTigre = false;
    23.  
    24.  
    25.         void Start()
    26.     {
    27.         AVentanaGuerrero = VentanaGuerrero.GetComponent<Animation>();
    28.         AVentanaGuitarra = VentanaGuitarra.GetComponent<Animation>();
    29.         AVentanaPiano = VentanaPiano.GetComponent<Animation>();
    30.         AVentanaTigre = VentanaTigre.GetComponent<Animation>();
    31.     }
    32.  
    33.     public void VentanaGuePulsada()
    34.     {
    35.         if (VisibleVGuerrero == false)
    36.         {
    37.             AVentanaGuerrero["Guerrero"].speed = 1;
    38.             AVentanaGuerrero.Play();
    39.             VisibleVGuerrero = true;
    40.         }
    41.     }
    42.  
    43.     public void VentanaGuiPulsada()
    44.     {
    45.         if (VisibleVGuitarra == false)
    46.         {
    47.             AVentanaGuitarra["Guitarra"].speed = 1;
    48.             AVentanaGuitarra.Play();
    49.             VisibleVGuitarra = true;
    50.         }
    51.     }
    52.  
    53.     public void VentanaPiaPulsada()
    54.     {
    55.         if (VisibleVPiano == false)
    56.         {
    57.             AVentanaPiano["Piano"].speed = 1;
    58.             AVentanaPiano.Play();
    59.             VisibleVPiano = true;
    60.         }
    61.     }
    62.  
    63.     public void VentanaTigPulsada()
    64.     {
    65.         if (VisibleVTigre == false)
    66.         {
    67.             AVentanaTigre["Tigre"].speed = 1;
    68.             AVentanaTigre.Play();
    69.             VisibleVTigre = true;
    70.         }
    71.     }
    72.  
    73.     public void VentanaNoPulsada()
    74.     {
    75.         string NombreBoton = EventSystem.current.currentSelectedGameObject.name;
    76.  
    77.         if (NombreBoton != "Guerrero" & VisibleVGuerrero == true)
    78.         {
    79.             AVentanaGuerrero["Guerrero"].speed = -1;
    80.             AVentanaGuerrero["Guerrero"].time = AVentanaGuerrero["Guerrero"].length;
    81.             AVentanaGuerrero.Play();
    82.             VisibleVGuerrero = false;
    83.         }
    84.  
    85.         if (NombreBoton != "Guitarra" & VisibleVGuitarra == true)
    86.         {
    87.             AVentanaGuitarra["Guitarra"].speed = -1;
    88.             AVentanaGuitarra["Guitarra"].time = AVentanaGuitarra["Guitarra"].length;
    89.             AVentanaGuitarra.Play();
    90.             VisibleVGuitarra = false;
    91.         }
    92.  
    93.         if (NombreBoton != "Piano" & VisibleVPiano == true)
    94.         {
    95.             AVentanaPiano["Piano"].speed = -1;
    96.             AVentanaPiano["Piano"].time = AVentanaPiano["Piano"].length;
    97.             AVentanaPiano.Play();
    98.             VisibleVPiano = false;
    99.         }
    100.  
    101.         if (NombreBoton != "Tigre" & VisibleVTigre == true)
    102.         {
    103.             AVentanaTigre["Tigre"].speed = -1;
    104.             AVentanaTigre["Tigre"].time = AVentanaTigre["Tigre"].length;
    105.             AVentanaTigre.Play();
    106.             VisibleVTigre = false;
    107.         }
    108.     }
    109. }
    110.  
    What's wrong with this old code I try to compile it into unity. I'm on unity version 2021.3.10f1.