Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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:
    36
    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:
    37
    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);
     
  8. moisturizor

    moisturizor

    Joined:
    Feb 1, 2017
    Posts:
    2
    Hello! When is this supposed to happen? Because I do really need this feature! Thanks
     
    Jason-RT-Bond likes 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 ?
     
  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.
     
unityunity