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.