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.

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 *= new ObjectReferenceKeyframe();*

_ spriteKeyFrames*.time = i;_
spriteKeyFrames_.value = sprites;
}
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();_

BUMP!! Anyone??

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 :slight_smile: