Display a Sprite in an EditorWindow

I have multiple spites in an atlas, I want display some of these in an EditorWindow.

	public static Sprite[] patterns;

	[MenuItem ("Window/Tile Selector")]
	static void Init () {
		patterns = Resources.LoadAll <Sprite> ("puzzle-pieces");  
		// Get existing open window or if none, make a new one:
		TileSelectWindow window = (TileSelectWindow)EditorWindow.GetWindow (typeof (TileSelectWindow));
		window.Show();
		Debug.Log ("Found: " + patterns.Length);
	}

This correctly displays Found: 9 but then running

	void OnGUI () {
		Texture2D aTexture = SpriteUtility.GetSpriteTexture(patterns[1], false);
		GUILayout.Label (aTexture);
	}

displays weirdly distorted texture atlas with ALL of the sprites…

Any way to display single sprites from multi sprite atlas?

The old GUI system isn’t ment to be used with sprites. The old GUI system usually expects single textures to be displayed. However you can use GUI.DrawTextureWithTexCoords and pass the appropriate texcoords to the method.

I haven’t tried this myself, but i think you should use Sprite.rect which gives you the texcoords in pixels. However DrawTextureWithTexCoords expects the coords to be in range (0…1). So you have to divide xMin and xMax by the texture’s width and yMin and yMax by the texture’s height.

This wilö work but is a bit complicated:

void DrawOnGUISprite(Sprite aSprite)
{
    Rect c = aSprite.rect;
    float spriteW = c.width;
    float spriteH = c.height;
    Rect rect = GUILayoutUtility.GetRect(spriteW, spriteH);
    if (Event.current.type == EventType.Repaint)
    {
        var tex = aSprite.texture;
        c.xMin /= tex.width;
        c.xMax /= tex.width;
        c.yMin /= tex.height;
        c.yMax /= tex.height;
        GUI.DrawTextureWithTexCoords(rect, tex, c);
    }
}

If you want to displays textures in the old GUI system, it’s better to have them as seperate textures. The old GUI system doesn’t do any drawcall batching so there’s no gain in using an atlas. Sprites are ment for the 2d framework or the new GUI system.

ps:
Note that my method uses the pixel size of the sprite to determine the size of the GUILayout rect. Ultimately you can use any screen rect to display the sprite’s section of the texture. Keep in mind that if you do the texture might get zoomed / stretched.

If you need / want to use that atlas to bring the textures into the project, you could also split the atlas manually and create seperate textures at start by copying the relevant pixels into a seperate texture.

edit

With thie little helper function you can generate a seperate texture from a Sprite:

Texture2D GenerateTextureFromSprite(Sprite aSprite)
{
    var rect = aSprite.rect;
    var tex = new Texture2D((int)rect.width, (int)rect.height);
    var data = aSprite.texture.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
    tex.SetPixels(data);
    tex.Apply(true);
    return tex;
}

Note: This method requires you to enable read / write access to the atlas texture. You have to switch the texture type to “advanced” and select “Read / Write Enable”. Otherwise the GetPixel call will fail.

if you just want to use it with EditorGUILayout handling position you can do it like that:

GUILayout.Box (spriteRef.texture);