Creating flipbook/spritelib animations programmatically

I’ve got a bunch of sprite assets (ZOMBIES!), which are animated sprites - each has several “actions” (walk, attack, die, etc.). I want to create a sprite library for each zombie with a category for each action and a label for each frame. I then want to create animation clips automatically for each action, with keyframes setting the category/label for each action frame. Each additional zombie color/variant would be in a “child” sprite library (so, same category/label set) and reference the master “parent” sprite library

My source frames look like this: {agent}{action}{frame}, for example, “zombie_blue_attack_000”, or “zombie_red_walk_004” and I use a regex to match the components, load the correct sprites, etc.

I’ve been able to create a sprite library “.asset” file, but I end up with the dialog to “upgrade to a .spriteLib”

Ideally, I’d have some kind of control ScriptableObject/asset, which would let me specify the regex to match, which actions to process, etc., and then it would generate the appropriate .spriteLib assets, animation clips, prefabs, etc.

Any hints or examples of doing this kind of thing would be great!

(The reason that I want to do it this way is that I can just change the spritelib component on the zombie game object to a different spritelib asset, and the spriteresolver will take care of everything else; I realize that it’s not the most performant way to get a sprite to animate, but … when you have a hammer all your problems look like nails :slight_smile: – if there’s a better way, I’m all ears – but, as an indicator of scale, I have 10 different zombies, with 5 different animations each, each animation having between 5 and 10 frames. And that’s just the zombies … )

– pryankster

It sounds like you need to get into editor scripting. There’s tons of basic tutorials out there to get you started.

After you get the basics going, what you describe is simply iterating more and more functionality into the system you are building. You’ll need to get started before you even figure out what ultimate shape / structure that system will have.

The main caveat when you are creating assets is to make sure a bug in your editor scripting doesn’t instantly wipe out assets in your project by incorrectly overwriting them. Always work with source control to guard against this problem.

yeah, I know that it involves editor scripting – and I have written, and have examples of ScriptedImporters, I’m specifically asking about the nuances around SpriteLibraryAsset vs. SpriteLibrarySourceAsset. I’d really like to create a single asset that includes the SpriteLibraryAsset object(s) (or SpriteLibrarySourceAssets? that’s one of the questions) as well as the animation clips and prefabs to utilize them.

–pryankster

The difference between a Sprite Library Asset and a Sprite Library Source Asset is that the first is a runtime asset, and the second is an editor asset. If we use textures as an example, Sprite Library Asset is equivalent to a Texture object and Sprite Library Source Asset is equivalent to a .png file. So when a Sprite Library Source Asset is imported into Unity, the SpriteLibrarySourceAssetImporter generates a Sprite Library Asset.

Currently, generating a Sprite Library Source Asset is an internal call. We have received feedback to make this call public in future versions of 2D Animation to enable developers to generate these assets more easily.

When you say

what kind of end result are you looking for? A main asset and a few sub assets (much like how the end result of a .psb/.psd file when imported with the PSD Importer)?

What I’m looking for is: a main asset with a few settings (i.e.: a name prefix for the generated assets, a regex for extracting variant, action and frame from the name of the PNG file, etc.). And that creates a set of assets: a main spritelib for the ‘default’ character, a sub-spritelib for each variant, a prefab with a game object, sprite library and sprite resolver, an animation clip for each ‘action’ setup with keyframes for each sprite frame and an animation controller with each of the animation clips added to it. The animation controller should be created on demand as a separate resource, only if it doesn’t already exist. If it does exist, only animation clips that are not already part of the animation controller should be added to it.

(I know it seems like a lot, but there are lots of tedious steps that I go through to add a new character or enemy to my game, and this would shorten the tedium immensely – that’s the point of editor scripting, right? :slight_smile:

– pryankster

Seems like you want to create a Scripted Importer. Take a look at this documentation page to see if it can get you going. You can also look at SpriteLibrarySourceAssetImporter.cs (located inside the 2D Animation package) to see how our Scripted Importer for .spriteLib file looks like, and PSDImporter.cs (located inside the PSD Importer package) for a more advanced usage of Scripted Importers.