How AnimationCurve.MoveKey works?

Hey there.

I’m writing a timeline editor, using AnimationCurves, and in this editor, I want to be able to move the keyframes over time. I could use the default AnimationCurve editor, but I need to have a view of the timeline’s range for all keyframes of all curves at a same time to be able to sync different curves together.

One way to move a keyframe within the time is by deleting it and adding it with its new time, but it is what the function MoveKey specificaly do.

BUT, while using it, it delete some keyframes without adding them, and I don’t understand the return value.

The documentation is also reaaaally minimalist, without example or whatsoever :

So, does anyone here understood the way one can move a keyframe safely? (and in another way, multiple keyframes alltogether?)

public AnimationCurve curve;
// ...
for(int key = 0; key < curve.length; key++)
	Keyframe temp = curve[key];
	temp.time = GetNewTime();
	curve.MoveKey(key, temp);

An AnimationCurve has to order all it’s keyframes by the keyframes time. If you change the time of a keyframe to a value of another keyframe those keyframes will be “merged” since it’s not possible to have two keyframes at the exact same time.

I never used MoveKey, but from the description it seems it takes care of the sorting and merge the keyframe with another one if they share the same time.

The function should in any case return the new index of the keyframe.

Your for loop is a bit of a risk since MoveKey can change the order of the array. You should use the keys property instead.

store a local copy of your keyframes, change them as you like and assign them back. When you assign the temp array back the keyframes will be sorted automatically.