Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unable to build AnimationClip at runtime

Discussion in 'Scripting' started by GnarleyMidget, Jan 29, 2016.

  1. GnarleyMidget

    GnarleyMidget

    Joined:
    Nov 6, 2015
    Posts:
    4
    I need to be able to dynamically build usable AnimationClips at runtime based on data that's unknown at build time. I'm currently using Version 5.2.4.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Experimental.Director;
    3. public class PivotController : MonoBehaviour {
    4.     void Start () {
    5.         var posxKeys = new Keyframe[3];
    6.         posxKeys[0] = new Keyframe( 0.0f, 0.0f);
    7.         posxKeys[1] = new Keyframe( 4.0f, 1.0f);
    8.         posxKeys[2] = new Keyframe( 8.0f, 0.0f);
    9.         var curve = new AnimationCurve(posxKeys);
    10.  
    11.         var clip = new AnimationClip() { legacy = false };
    12.         clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
    13.  
    14.         Animator animator = gameObject.AddComponent<Animator>();
    15.         var mixer = new AnimationMixerPlayable();
    16.         mixer.AddInput(new AnimationClipPlayable(clip));
    17.         animator.Play(mixer);
    18.     }
    19. }
    20.  
    This controller works when applied to a gameObject and run in the editor. The same code fails when compiled and executed, with the following error -

    "Can't use AnimationClip::SetCurve at Runtime on non Legacy AnimationClips"


    If I set legacy = true the error messages go away, but the animation then fails in both Editor and Runtime.

    There has to be some way of doing this in a stand alone executable. In a somewhat related issue, there also seems to no way of creating a looping clip at runtime (clip.wrapMode = WrapMode.Loop doesn't do it). As near as I can tell the only way to do this is to serialize the Animation clip, modify the property, and reapply - which is not an option outside of the Editor. Why are there no dynamic runtime options available?
     
  2. pierrepaul

    pierrepaul

    Unity Technologies

    Joined:
    Jun 19, 2012
    Posts:
    162
    Its currently not possible to create Animation at runtime ( unless using Legacy )

    We are working on adding that soon.
     
  3. GnarleyMidget

    GnarleyMidget

    Joined:
    Nov 6, 2015
    Posts:
    4
    I'm willing to live with Legacy animationClips for the time being, but I don't seem to be able to make that work either.

    What would I have to add to the above sample (besides legacy = true) in order to get it to work?
     
  4. GnarleyMidget

    GnarleyMidget

    Joined:
    Nov 6, 2015
    Posts:
    4
    Ok - fixed it. I was attempting to use the Animator component to play a legacy clip. If I use the Animation component instead then this sample works at Runtime. Thanks.

    Animation animation = gameObject.AddComponent<Animation>();
    animation.AddClip(clip, "test");
    animation.Play("test");
     
    deus0 likes this.
  5. commodore

    commodore

    Joined:
    May 30, 2012
    Posts:
    39
    Cool, do you know when we will be able to do it? I'm currently able to create animations at runtime in the editor
     
  6. deus0

    deus0

    Joined:
    May 12, 2015
    Posts:
    256
    I am also waiting on this! I believe its been a couple of years haha
     
  7. Lawlets

    Lawlets

    Joined:
    Mar 1, 2018
    Posts:
    1
    @deus0 It's work now. You just have to set your animationClip legacy to true before call the setCurve function


    Code (CSharp):
    1. AnimationClip animClip = new AnimationClip();
    2. animClip.legacy = true;
    3. animClip.SetCurve("", typeof(), $"{nameof()}", YourAnimationCurve);
     
    deus0 likes this.
  8. moisturizor

    moisturizor

    Joined:
    Feb 1, 2017
    Posts:
    2
    Hello! When is this supposed to happen? Because I do really need this feature! Thanks
     
    mcl-dev and Jason-RT-Bond like this.
  9. rickknowles

    rickknowles

    Joined:
    Apr 21, 2018
    Posts:
    7
    Ditto - can someone from unity explain the limiting factor please ? It seems like the sort of thing that isnt depending on specific hardware acceleration etc. Is it a licensing issue ?
     
    TobyWu likes this.
  10. rickknowles

    rickknowles

    Joined:
    Apr 21, 2018
    Posts:
    7
    This is doubly important given
    a) how much Unity has been hyping Timeline as being the way to manage animations and
    b) the fact that Timeline doesn't support legacy animations at all (it rejects the clips on the standard animation tracks)

    I have a set of custom timeline playable assets that except for this would be able to runtime generate the animations I need, but because of this seemingly arbitrary limitation it's all unusable.
     
    mcl-dev likes this.
  11. ValiantTechStudios

    ValiantTechStudios

    Joined:
    May 10, 2020
    Posts:
    7
    This is something that I really need. I can't believe that coming soon is now 4 years ago. When exactly is soon?
     
  12. morph_jhKim

    morph_jhKim

    Joined:
    Aug 28, 2020
    Posts:
    4
    We are working on adding that soon.???

    Ok... I am still waiting.
     
    mcl-dev, craftsmanbeck and Sprawl like this.
  13. mcl-dev

    mcl-dev

    Joined:
    Feb 22, 2016
    Posts:
    5
    more than 5 years is soon for Unity apparently
     
  14. Ben1138

    Ben1138

    Joined:
    Mar 17, 2013
    Posts:
    2
    This feature is REALLY needed! Does someone know a workaround for this, besides writing some sort of manual transform manipulation from scratch....
     
    alvaro_perez_omnidrone and deus0 like this.
  15. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
  16. Tulrath

    Tulrath

    Joined:
    Feb 1, 2015
    Posts:
    10
    Any updates on this? This issue has been open now for almost 6 years.
     
  17. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    The unity dev that responded to this back in 2016 hasn't posted on the forums since 2019 so probably doesn't even work there anymore. I seriously doubt this is even on their radar.
     
  18. Abhiwan_Pawan

    Abhiwan_Pawan

    Joined:
    Jun 16, 2021
    Posts:
    2
    Can't use AnimationClip::SetCurve at Runtime on non-legacy AnimationClips

    _JS_Log_Dump @ Build.framework.js:2.
    How long we have to wait for?
     
    LuLusg and aurelioprovedo like this.
  19. TigerHix

    TigerHix

    Joined:
    Oct 20, 2015
    Posts:
    53
    Unity: Soon™ we will implement this very important feature, but we think acquiring 100 companies first is more important...
     
  20. MingZiJiaoZhongGuo

    MingZiJiaoZhongGuo

    Joined:
    Apr 3, 2021
    Posts:
    2
    In 2022, you can only create legacy animation at runtime with Unity, what a time to be alive!
     
    Pblu and TigerHix like this.