So in the past few days I’ve been talking with my colleagues about the history and API changes of Sprite Library and its friends. I’ll write down some insights here to correct some of my previous statements and also to update our collective perception of the feature.
So in Unity we have two types of assets, “source assets” such as .psd, .png and fbx; and “runtime assets” such as Texture, Sprite and Mesh. When a source asset is imported into Unity, an importer associated with the file ending of the source asset takes over to read the content of the file and its meta file, and generates one or more runtime assets. For a .png it looks something like this:
A .png is added into a Unity project → Texture Importer reads the .png and its .meta file → A Texture asset (and sometimes Sprite assets) is generated.
In Unity 2021.1, we updated Sprite Library Asset to follow the same design. We introduced the .spriteLib as Sprite Library’s source asset, and kept the Sprite Library Asset as its runtime asset.
The benefit of this design mainly comes down to decoupling the source asset from the runtime asset. With this decoupling, we are able to:
- Modify the data without touching the source asset data (e.g. apply compression and remove unnecessary data not needed in runtime).
- Allowing different source assets to generate the same runtime assets (e.g. .png, .psd and .tga all have a Texture generated on import by the TextureImporter).
- Allowing the same source assets to generate different runtime assets (e.g. a .psd can be processed by the TextureImporter or the PSD Importer, which will generate a different set of assets).
One common API structure these pipelines share is that Unity tends to not have an API in place to generate source assets, but has an API in place to generate runtime assets. E.g. a Texture can be generated by creating a new Texture2D object, a Mesh can be generated by creating a new Mesh object and a Sprite can be generated by using the Sprite.Create method. The same thing goes for a Sprite Library Asset. Here is some code to showcase how you could create a new Sprite Library Asset and save it on disk:
public static class SpriteLibCreator
{
[MenuItem("Tools/Create Sprite Lib")]
static void CreateLib()
{
const string spriteLibName = "MySpriteLib.asset";
var spriteLib = ScriptableObject.CreateInstance<SpriteLibraryAsset>();
spriteLib.AddCategoryLabel(null, "Cat", "Label");
AssetDatabase.CreateAsset(spriteLib, "Assets/" + spriteLibName);
}
}
You could also go more advanced, and create your own source asset with its unique file ending and create a custom importer for it. This would allow you to author Sprite Library data outside (or inside) of Unity and then generate it exactly how you would like it.
During my dive into this area, I saw that SpriteLibraryAsset.CreateAsset
is internal. To follow the same design as the other pipelines, I believe we should make this a public method to ease the way a Sprite Library Asset can be generated.
Apart from this change, are there any other changes to the Sprite Library Asset API that you would like to see?