SpriteCollection is a convenient way to load sprites dynamically in Unity. The reason we use it instead of just putting the sprites in Resources
folder is because sprites in Resources
folder cannot be packed in atlases.
There are 2 main ways to used SpriteCollection: To represent all the sprites of all textures in 1 folder OR To represent all the sprites of a single texture.
Case 1: The folder way
-
Create a prefab with one component
SpriteCollection
for each folder of sprites. For instance, if you have a folder of icons inAssets/Texture/Icons
, you can create a SpriteCollection prefab calledIconSpriteCollection
-
Fill in the correct folder name, e.g. if your folder is
Assets/Textures/Icons
, you would typeTextures/Icons
intoFolder Name
-
Click
Reload
, this will populate theSprites
array with the correct sprites. You can also changeSprites
manually in Editor. -
In game, use
SpriteCollectionManager
to retrieveSpriteCollection
. First you have to callSpriteCollectionManager.CreateInstance ()
once at the beginning of the game -
Then you can load appropriate
SpriteCollection
when needed. For instance, if you have aSpriteCollection
prefab calledSpriteCollections/Icons
insideResources
, you would call:var spriteCollection = SpriteCollectionManager.Instance.GetCollection ("SpriteCollections/Icons");
-
Alternatively, you can load a
SpriteCollection
from AssetBundle or StreamingAsset and set it to theSpriteCollectionManager
:var spriteCollection = ... // load from AssetBundle SpriteCollectionManager.Instance.SetCollection ("SpriteCollections/Icons", spriteCollection);
-
Use the
SpriteCollection
like so:spriteCollection.GetSprite ("Bracer");
Case 2: The texture atlas way
- Same as Case 1
- In Editor, Drag the original texture to the
Texture
field of the SpriteCollection - Click
Reload
, this will populate theSprites
array with all the sprites in the texture. If the texture Sprite Mode isSingle
or if the texture is not a sprite, only 1 sprite will be loaded - Same as Case 1
- Same as Case 1
- Same as Case 1
- Same as Case 1
There's a sample scene called Test
for you to try.
To include SpriteCollection into your project, you can use npm
method of unity package management described here.
0.0.4
- Add API
SpriteCollectionManager::SetCollection
to set a collection loaded from elsewhere.
0.0.3
- Add support for multiple sprites in a texture.
- Add supprot for a single texture
0.0.2
- Fix potential confusing namespace Editor
0.0.1
- Initial commit