Search code examples
c#unity-game-engine2dtexturessprite

Get single sprite from sprite atlas


I'd like to get a single sprite which GameObject has in a SpriteRenderer component. Unfortunately, this code returns the whole atlas, but I need to a part of this atlas.

Texture2D thumbnail = GetComponent<SpriteRenderer>().sprite.texture;

Pic


Solution

  • There is no native API to get single sprite from the SpriteRenderer and no API to access individual Sprite by name. You can vote for this feature here.

    You can make your own API to get single sprite from Atlas located in the Resources folder like the image included in your question.

    You can load all the sprites from the Atlas with Resources.LoadAll then store them in a dictionary.A simple function can then be used to access each sprite with the provided name.

    A simple Atlas Loader script:

    public class AtlasLoader
    {
        public Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();
    
        //Creates new Instance only, Manually call the loadSprite function later on 
        public AtlasLoader()
        {
    
        }
    
        //Creates new Instance and Loads the provided sprites
        public AtlasLoader(string spriteBaseName)
        {
            loadSprite(spriteBaseName);
        }
    
        //Loads the provided sprites
        public void loadSprite(string spriteBaseName)
        {
            Sprite[] allSprites = Resources.LoadAll<Sprite>(spriteBaseName);
            if (allSprites == null || allSprites.Length <= 0)
            {
                Debug.LogError("The Provided Base-Atlas Sprite `" + spriteBaseName + "` does not exist!");
                return;
            }
    
            for (int i = 0; i < allSprites.Length; i++)
            {
                spriteDic.Add(allSprites[i].name, allSprites[i]);
            }
        }
    
        //Get the provided atlas from the loaded sprites
        public Sprite getAtlas(string atlasName)
        {
            Sprite tempSprite;
    
            if (!spriteDic.TryGetValue(atlasName, out tempSprite))
            {
                Debug.LogError("The Provided atlas `" + atlasName + "` does not exist!");
                return null;
            }
            return tempSprite;
        }
    
        //Returns number of sprites in the Atlas
        public int atlasCount()
        {
            return spriteDic.Count;
        }
    }
    

    Usage:

    enter image description here

    With the Example image above, "tile" is the base image and ball, bottom, people, and wallframe are the sprites in the atlas.

    void Start()
    {
        AtlasLoader atlasLoader = new AtlasLoader("tiles");
    
        Debug.Log("Atlas Count: " + atlasLoader.atlasCount());
    
        Sprite ball = atlasLoader.getAtlas("ball");
        Sprite bottom = atlasLoader.getAtlas("bottom");
        Sprite people = atlasLoader.getAtlas("people");
        Sprite wallframe = atlasLoader.getAtlas("wallframe");
    }