- Home /
How to edit Animation Clip Settings using an editor script
Hi Everyone, I'm trying to create an animation clip that loops using an editor script. I can do everything except loop the script. I have tried setting the wrapMode to WrapMode.Loop. I have also used AnimationUtility.Get/Set AnimationClipSettings. neither works...If I use the following code, I can see the "Loop Time" checkbox enabled in the inspector, but the animation does not loop. Is it a bug in Unity, or am I doing something wrong?
AnimationClip newClip = new AnimationClip();
AnimationClipSettings acs = AnimationUtility.GetAnimationClipSettings(animClip);
AnimationClipSettings newSettings = AnimationUtility.GetAnimationClipSettings(newClip);
animClip.wrapMode = WrapMode.Loop;
newSettings.cycleOffset = acs.cycleOffset;
newSettings.heightFromFeet = acs.heightFromFeet;
newSettings.keepOriginalOrientation = acs.keepOriginalOrientation;
newSettings.keepOriginalPositionXZ = acs.keepOriginalPositionXZ;
newSettings.keepOriginalPositionY = acs.keepOriginalPositionY;
newSettings.level = acs.level;
newSettings.loopBlend = acs.loopBlend;
newSettings.loopBlendOrientation = acs.loopBlendOrientation;
newSettings.loopBlendPositionXZ = acs.loopBlendPositionXZ;
newSettings.loopBlendPositionY = acs.loopBlendPositionY;
newSettings.loopTime = true;
newSettings.mirror = acs.mirror;
newSettings.orientationOffsetY = acs.orientationOffsetY;
newSettings.startTime = acs.startTime;
newSettings.stopTime = acs.stopTime;
newSettings.additiveReferencePoseClip = acs.additiveReferencePoseClip;
newSettings.additiveReferencePoseTime = acs.additiveReferencePoseTime;
newSettings.hasAdditiveReferencePose = acs.hasAdditiveReferencePose;
//ModelImporterClipAnimation blah = animClip as ModelImporterClipAnimation;
AnimationUtility.SetAnimationClipSettings(animClip, newSettings);
Answer by Adam-Mechtley · Nov 16, 2016 at 09:51 AM
If "Loop Time" is true, that should be sufficient. Can you confirm that it remains ticked when the application starts playing (and the animation appears to not loop)
Hi Adam, thanks for the reply.
Yes, that is exactly how it is happening. If I run the code above from the editor, it ticks the "Loop Time" checkbox for the animation. When I press play, the animation plays once in the Scene/Game view, but does not play again. Interestingly, if I look at the animation state in the Animator window, I can see that it is trying to play on repeat (the status bar is looping for the animation state), but in the actual Scene/Game view, the animation is not looping.
Furthermore, if I simply uncheck and check "Loop Time" manually in the editor (with my mouse), it starts to play on loop. This seems like a Unity bug, but I was wondering if you had any thoughts...
Thanks...
What kind of EditorScript is this happening in? $$anonymous$$g., AssetPostprocessor? You might try this and see if it makes a difference:
Undo.RecordObject (animClip, "Change Settings");
AnimationUtility.SetAnimationClipSettings (animClip, newSettings);
EditorUtility.SetDirty (animClip);
This is happening in an "EditorWindow" script (I hope that answers your question).
I tried your recommendation above, but it did not work. It did change something though - The check mark for Loop time is still checked, but now when I am viewing the animation, it does not loop, but it also does not look like it's trying to loop in the animator window either...(the blue line does not keep resetting).
Sorry for my delayed response also...I had to move off this issue to gain progress elsewhere...
Any thoughts? thanks for the help
Answer by Jonesy19 · Dec 22, 2016 at 04:41 PM
I submitted a bug for this. The Case ID is 864049.
Thanks for the help Adam.