Create Animation Clip from Sprites[] (Programmatically)
Hello! I have been working on a lite codeless RPG Game Engine which I am building in Unity, using the windows editors that Unity provides. I am have been looking how to change an array of Sprites to an AnimationClip and I don't quite understand it.
I have seen in previous posts (like this one) that you have to use the method "SetCurve". However, in the post mentioned I don't see where does he references that AnimationCurve with the Array of sprites that you want to reproduce the clip.
In short, I would like to reproduce what you do when you select multiple sprites and you drag it to the Hierarchy window (it automatically produces the animation), but in code.
Answer by BauPlay · Oct 19, 2015 at 12:26 PM
Hi! may be this
Save your spritesheet in assets/Resources/sprite
Sprite[] sprites = Resources.LoadAll<Sprite>("sprite"); // load all sprites in "assets/Resources/sprite" folder
AnimationClip animClip = new AnimationClip();
animClip.frameRate = 25; // FPS
EditorCurveBinding spriteBinding = new EditorCurveBinding();
spriteBinding.type = typeof(SpriteRenderer);
spriteBinding.path = "";
spriteBinding.propertyName = "m_Sprite";
ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[sprites.Length];
for(int i = 0; i < (sprites.Length); i++) {
spriteKeyFrames[i] = new ObjectReferenceKeyframe();
spriteKeyFrames[i].time = i;
spriteKeyFrames[i].value = sprites[i];
}
AnimationUtility.SetObjectReferenceCurve(animClip, spriteBinding, spriteKeyFrames);
//------------------Now if want, you can save AnimationClip to file .anim
AssetDatabase.CreateAsset(animClip, "assets/walk.anim");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Thanks for the code. I have one last question regarding the .anim file, I am trying to create it with loop time checked by default and I thought it was like this:
animClip.wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
However, when I check the file, it's not checked the loop time property.
I do not know how to change Wrap$$anonymous$$ode. Also I think about it...
for loop animation, i use this code
AnimationClipSettings animClipSett = new AnimationClipSettings();
animClipSett.loopTime = true;
AnimationUtility.SetAnimationClipSettings(animClip, animClipSett);
sorry for my bad english)
Hi ! Awsome, how did you find out ? I'm trying the same, but i am getting blank sprites in the animation like this Any idea why ? Im doing this on import via OnPostprocessSprites(Texture2D texture, Sprite[] sprites).
Solved thanks to this post: https://answers.unity.com/questions/1356394/loading-sprite-from-sprite-sheet.html
Sprites which was being given to me via OnPostprocessSprites(Texture2D texture, Sprite[] sprites) were not valid and actually had negative InstanceID's. So the trick was to load the sprites manually and get the right references.
Answer by Arkasis · Oct 30, 2020 at 03:20 PM
Hello guys, I got some trubbles creating my own animationclip by code.
Eveything is working fine in play mode.
But in the animation inspector, the animationClip is recognize as if it only have a single frame. I have to manually do a random modification on the curves or the frame rate in the inspector in order to make Unity refresh it. It is like if Unity was thinking the animation start and stop time were not good but they are okay. Is there a way to avoid this issue ?
PS : If I restart Unity, everything works but I would like to avoid it.... Thanks :)