Convert Sprite Image to Texture

I am rendering a GUI.Button which normally takes a Texture but I’d like to load the image for the button from a sprite sheet instead.

I’ve been digging in code for a while but I can’t find a way to cast/convert a sprite to a texture.

So if anyone is looking for the answer: here it is.

First, you have to open your sprite texture in the inspector and change the type from “Sprite” to “Advanced”. Then make sure that Read/Write Enabled is checked. Click Apply and now you can read the texture and set it into a new texture like so:

// assume "sprite" is your Sprite object
var croppedTexture = new Texture2D( (int)sprite.rect.width, (int)sprite.rect.height );

var pixels = sprite.texture.GetPixels(  (int)sprite.textureRect.x, 
                                        (int)sprite.textureRect.y, 
                                        (int)sprite.textureRect.width, 
                                        (int)sprite.textureRect.height );

croppedTexture.SetPixels( pixels );
croppedTexture.Apply();

croppedTexture will now contain the sprite’s image as a Texture2D!

Here is a static helper method that will give you a texture2D from a sprite. It will even determine if the sprite is part of a single texture or if it is just a single sprite automatically. (basically the same as codeimpossibles answer)

    public static Texture2D textureFromSprite(Sprite sprite)
	{
		if(sprite.rect.width != sprite.texture.width){
			Texture2D newText = new Texture2D((int)sprite.rect.width,(int)sprite.rect.height);
			Color[] newColors = sprite.texture.GetPixels((int)sprite.textureRect.x, 
			                                             (int)sprite.textureRect.y, 
			                                             (int)sprite.textureRect.width, 
			                                             (int)sprite.textureRect.height );
			newText.SetPixels(newColors);
			newText.Apply();
			return newText;
		} else
			return sprite.texture;
	}

For those looking for an Editor Only solution you can use AssetPreview.GetAssetPreview. If you really wanted to convert a Sprite to a Texture it must be Readable which means you have to set it to Advanced in the import settings and then make it readable. Doing so will make it consume more memory so you’ll have to weigh the pros and cons of going through all that mess.

If all you’re doing is drawing sprites in the editor, GetAssetPreview isn’t bad.

@username trothmaster

Your solution is very good, but the code returns a error of pixel count.

resolved using the System.Math.Ceiling for to round the number for up.

Texture2D ConvertSpriteToTexture(Sprite sprite)
            {
                try
                {
                    if (sprite.rect.width != sprite.texture.width)
                    {
                        Texture2D newText = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
                        Color[] colors = newText.GetPixels();
                        Color[] newColors = sprite.texture.GetPixels((int)System.Math.Ceiling(sprite.textureRect.x),
                                                                     (int)System.Math.Ceiling(sprite.textureRect.y),
                                                                     (int)System.Math.Ceiling(sprite.textureRect.width),
                                                                     (int)System.Math.Ceiling(sprite.textureRect.height));
                        Debug.Log(colors.Length+"_"+ newColors.Length);
                        newText.SetPixels(newColors);
                        newText.Apply();
                        return newText;
                    }
                    else
                        return sprite.texture;
                }catch
                {
                    return sprite.texture;
                }
            }
1 Like

if spriteAtlas

public static Texture2D textureFromSprite(Sprite sprite)
{
    if (sprite.rect.width != sprite.texture.width)
    {
        int texWid = (int)sprite.rect.width;
        int texHei = (int)sprite.rect.height;
        Texture2D newTex = new Texture2D(texWid, texHei);
        Color[] defaultPixels = Enumerable.Repeat<Color>(new Color(0, 0, 0, 0), texWid * texHei).ToArray();
        Color[] pixels = sprite.texture.GetPixels((int)sprite.textureRect.x
        , (int)sprite.textureRect.y
        , (int)sprite.textureRect.width
        , (int)sprite.textureRect.height);

        newTex.SetPixels(defaultPixels);
        newTex.SetPixels((int)sprite.textureRectOffset.x, (int)sprite.textureRectOffset.y, (int)sprite.textureRect.width, (int)sprite.textureRect.height, pixels);
        newTex.Apply();
        return newTex;
    }
    else
    {
        return sprite.texture;
    }
},if spriteAltas

public static Texture2D textureFromSprite(Sprite sprite)
{
    if (sprite.rect.width != sprite.texture.width)
    {
        int texWid = (int)sprite.rect.width;
        int texHei = (int)sprite.rect.height;
        Texture2D newTex = new Texture2D(texWid, texHei);
        Color[] defaultPixels = Enumerable.Repeat<Color>(new Color(0, 0, 0, 0), texWid * texHei).ToArray();
        Color[] pixels = sprite.texture.GetPixels((int)sprite.textureRect.x
        , (int)sprite.textureRect.y
        , (int)sprite.textureRect.width
        , (int)sprite.textureRect.height);

        newTex.SetPixels(defaultPixels);
        newTex.SetPixels((int)sprite.textureRectOffset.x, (int)sprite.textureRectOffset.y, (int)sprite.textureRect.width, (int)sprite.textureRect.height, pixels);
        newTex.Apply();
        return newTex;
    }
    else
    {
        return sprite.texture;
    }
}

You can simply use Resources.Load. Since asset type is determined not by file, but by Unity itself, you can use your sprite source image as any graphic asset. Using C#

Texture tex = Resources.Load (“textureName”);