Skip to content

SpriteCollection is a convenient way to load sprites dynamically in Unity

Notifications You must be signed in to change notification settings

minhhh/UBootstrap.SpriteCollection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SpriteCollection

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.

Usage

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

  1. Create a prefab with one component SpriteCollection for each folder of sprites. For instance, if you have a folder of icons in Assets/Texture/Icons, you can create a SpriteCollection prefab called IconSpriteCollection

  2. Fill in the correct folder name, e.g. if your folder is Assets/Textures/Icons, you would type Textures/Icons into Folder Name

  3. Click Reload, this will populate the Sprites array with the correct sprites. You can also change Sprites manually in Editor.

  4. In game, use SpriteCollectionManager to retrieve SpriteCollection. First you have to call SpriteCollectionManager.CreateInstance () once at the beginning of the game

  5. Then you can load appropriate SpriteCollection when needed. For instance, if you have a SpriteCollection prefab called SpriteCollections/Icons inside Resources, you would call:

    var spriteCollection = SpriteCollectionManager.Instance.GetCollection ("SpriteCollections/Icons");
    
  6. Alternatively, you can load a SpriteCollection from AssetBundle or StreamingAsset and set it to the SpriteCollectionManager:

    var spriteCollection = ... // load from AssetBundle
    SpriteCollectionManager.Instance.SetCollection ("SpriteCollections/Icons", spriteCollection);
    
  7. Use the SpriteCollection like so:

    spriteCollection.GetSprite ("Bracer");
    

Case 2: The texture atlas way

  1. Same as Case 1
  2. In Editor, Drag the original texture to the Texture field of the SpriteCollection
  3. Click Reload, this will populate the Sprites array with all the sprites in the texture. If the texture Sprite Mode is Single or if the texture is not a sprite, only 1 sprite will be loaded
  4. Same as Case 1
  5. Same as Case 1
  6. Same as Case 1
  7. 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.

Changelog

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

About

SpriteCollection is a convenient way to load sprites dynamically in Unity

Resources

Stars

Watchers

Forks

Packages

No packages published