Altering animation curves via script
Hi, I'm trying to write an editor extension that alters an animation curve via script. Unfortunately, any attempt I make to silently alter the value of the animation clip fails silently.
I first tried using curve.AddKey but this fails to change the value but that failed to change it (no error, just no change), so I tried altering the keyframe directly. Stepping over the line ' curve.keys[i].value = value;', I can see in the debug watch that 'curve.keys[i].value' never changes - it's as if the 'set' property method is implemented but does nothing.
void FindOrAddKey(AnimationCurve curve, float time, float value)
{
for (int i = 0; i < curve.keys.Length; ++i)
{
if (curve.keys[i].time == time)
{
curve.keys[i].value = value;
return;
}
}
curve.AddKey(time, value);
}
My question is if this is intentional or if it's a bug in Unity? Also, is there another way to edit the curve (I can't find a way to remove a curve from an animation either - there's no opposite of 'AnimationUtility.SetEditorCurve' as far as I can see.
Thanks for your time.
Answer by timuther · Mar 07, 2016 at 04:12 PM
got it - you have to remove the key and re-add, like this: static void FindOrAddKey(AnimationCurve curve, float time, float value) { for (int i = 0; i < curve.keys.Length; ++i) { if (curve.keys[i].time == time) {
curve.RemoveKey(i);
break; } }
int key = curve.AddKey(time, value); }
Answer by jaredDumpling · Apr 24, 2019 at 03:25 AM
For anyone else looking, there is a simpler method of modifying key values without having to remove/add.
Keyframe[] keyframes = animationCurve.keys;
keyframes[0].value = 0;
keyframes[1].value = 1;
keyframes[2].value = 0;
animationCurve.keys = keyframes;
Your answer
Follow this Question
Related Questions
Prevent keyframe interpolation in imported model animation in Unity 1 Answer
Can't add animated properties to a 3D character in 2018 0 Answers
Animation Rigging works differently for different animations 0 Answers
Timeline character animation cancels out the character script gravity logic 1 Answer
Can't attach animationClip to animation 0 Answers