0
votes

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

3

3 Answers

0
votes

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");
}
-2
votes

You could put the image you need in the Resources folder by itself, then use the Resources.Load("spriteName") to get it. If you want to get it as a sprite, you would do the following:

Sprite thumbnail = Resources.Load("spriteName", typeof(Sprite)) as Sprite;

Source: https://forum.unity3d.com/threads/how-to-change-sprite-image-from-script.212307/

-3
votes

Well with the new Unity versions you can do it easily using SpriteAtlas class and GetSprite method: https://docs.unity3d.com/ScriptReference/U2D.SpriteAtlas.html

So if you are working with the Resources folder you can do:

Resources.Load<SpriteAtlas>("AtlasName")