# Create animation clips programatically

I am messing around with Unity and the LPC Spritesheet Collection.

The LPC sprites are a set of artworks all of the same size and the same amount of animations. I can create animations for it no problem, when I do it manually.

My issue is: LPC has a lot of different character bodies (skin colors, different races), since they are of the same size and have the same animations in the same way, I was trying to create an AssetPostProcessor extension that would create the animations automatically when I import a LPC image into my project.

I am already able to slice the spritesheets correctly with this Script I found, and I can create and save the animation clips based on the spritesheets. The clips have the correct length and configuration, but the sprites for each frame seem to be empty! Take a look at the image below...

This is the piece of code that generates the animations from the sprite meta datas:

    for (int col = 0; col < m_ColCount; ++col)
{
meta.rect = new Rect(col * LPC_SPRITE_SIZE, row * LPC_SPRITE_SIZE, LPC_SPRITE_SIZE, LPC_SPRITE_SIZE);

// I skipped some code for renaming and checking for empty sprite frames for brevity...

EditorCurveBinding spriteBinding = new EditorCurveBinding();
spriteBinding.type = typeof(SpriteRenderer);
spriteBinding.path = "";
spriteBinding.propertyName = "m_Sprite";

ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[currentLoops.Count];
for (int i = 0; i < currentLoops.Count; i++)
{
spriteKeyFrames[i] = new ObjectReferenceKeyframe();
spriteKeyFrames[i].time = i;
spriteKeyFrames[i].value = currentLoops[i];
}

AnimationClip clip = new AnimationClip();
clip.name = animState.ToString();
clip.frameRate = currentLoops.Count;

AnimationUtility.SetObjectReferenceCurve(clip, spriteBinding, spriteKeyFrames);

if(!changesToSave.ContainsKey("Assets/Sprites/" + clip.name + ".anim"))
changesToSave.Add("Assets/Sprites/" + clip.name + ".anim", clip);

}
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.spritesheet = metas.ToArray();


And I am saving the sprites in the OnPostProcessSprites callback like this:

Debug.Log("Sliced Sprites: " + sprites.Length);

foreach (var key in changesToSave.Keys)
{
AssetDatabase.CreateAsset(changesToSave[key], key);
}

var textureImporter = (TextureImporter)assetImporter;
Debug.Log(textureImporter.spritesheet.Length);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

• If there's no difference between the animation data for each of these sets of sprites, then you might not want to create new animations at all. Setting up a script to always run the same animation, but swap the sprites displayed, would likely be more efficient. Feb 2 '20 at 21:52
• @DMGregory hmmm sounds interesting, but how would that work? I know I can set the sprite in the spriterenderer component, but how would I do that with a sliced spritesheet? Feb 2 '20 at 22:21
• @DMGregory Thanks for your insight, I figured it out and implemented my own sprite swapper for this case! Feb 3 '20 at 0:25
• I'd recommend editing your question to something like "How do I animate many sets of sprites with the same animation?" And add the solution you developed as an Answer. Feb 3 '20 at 2:15