I’m currently trying to implement quite a simple system where I can offset all the keyframes after a particular specified time by a set amount on any given animation on any object that has animations.
The part that I’m struggling with currently is that the functionality of AnimationCurve.RemoveKey(), AnimationCurve.AddKey() and AnimationCurve.MoveKey() don’t seem to do at all what they should do. Remove Key and Move Key flat out do not delete the specified Key. By using Move Key, it simply moves the key to the new location, but leaves another key where the old one was. Simply using remove key appears to do nothing. And whilst the new keys and their positions do appear on the animation curve when accessing it through the animation editor, the new keys added through Move Key or Add Key are not in the AnimationCurve.keys array.
Here is my current code (This assumes that curves is a list of AnimationCurves, and curveData is an array of AnimationClipCurveData. Selected Time is the time specified by the user to push all keyframes after back, and pushTime is the amount each keyframe after the specified time is to be push back by):
for (int i = 0; i < curves.Count; i++)
{
AnimationCurve currentCurve = curves*;*
-
for (int j = 0; j < currentCurve.keys.Length; j++)*
-
{*
-
Keyframe currentKey = currentCurve.keys[j];*
-
if (currentKey.time > selectedTime)*
-
{ *
-
currentKey.time += pushTime;*
-
currentCurve.RemoveKey(j);*
-
if (j >= currentCurve.keys.Length)*
-
{*
-
currentCurve.AddKey(currentKey);*
-
}*
-
else*
-
{*
-
currentCurve.MoveKey(j, currentKey);*
-
}*
-
}*
-
}*
currentState.clip.SetCurve(curveData.path, curveData_.type, curveData*.propertyName, currentCurve);
}*
Any help would be appreciated._