I’ve made a script to record some objects’ behaviours to an AnimationClip. It uses AnimationClip.SetCurve(). All curves are equal in length and number of keys. Sometimes, more often than not, after setting the curves the “Stop Time” parameter of animation Clip Settings (it’s visible in Debug mode, in the inspector) gets set to 1, regardless of the actual animation length. Using the animation window everything plays back normally, but during runtime only the first second of animation is played. Setting the Stop Time by hand to a proper value fixes it, but it’s uncomfortable doing it every time. Plus, I’d really like to know why that happens, so if anyone has any ideas I’d be grateful for sharing them. Thanks.
Ok, I’ve determined that calling AnimationClip.ClearCurves() resets the parameter in question. Deleting curves by hand in the animation window doesn’t seem like a great idea. Is there a way to set the “Stop Time” manually, after it gets reset?
The problem is solved with some workaround. Basically there’s no way setting the StopTime at runtime, without using UnityEditor.AnimationUtility.SetEditorCurve(). Luckily, adding an event to an animation clip seems to solve all problems. The event’s time has to be greater or equal to the animation’s length. Events added in runtime are non-persistent, so exitting the playmode removes them, adjusting the StopTime to the clip’s length.