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.
using UnityEngine;
using UnityEngine.Experimental.Director;
public class PivotController : MonoBehaviour {
void Start () {
var posxKeys = new Keyframe[3];
posxKeys[0] = new Keyframe( 0.0f, 0.0f);
posxKeys[1] = new Keyframe( 4.0f, 1.0f);
posxKeys[2] = new Keyframe( 8.0f, 0.0f);
var curve = new AnimationCurve(posxKeys);
var clip = new AnimationClip() { legacy = false };
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
Animator animator = gameObject.AddComponent<Animator>();
var mixer = new AnimationMixerPlayable();
mixer.AddInput(new AnimationClipPlayable(clip));
animator.Play(mixer);
}
}
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?
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.
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 ?
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.
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.