- Home /
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?
Answer by Bunny83 · Apr 24, 2015 at 12:30 PM
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.
Answer by makaka-org · Feb 04, 2018 at 03:32 PM
☄ Header Image in Publisher Window
Publisher Window is a welcome screen where the user can learn all the important information about the product (unity asset or project) and your company by visiting important links.
☄ Download: http://u3d.as/HKG
☄ Documentation: https://makaka.org/publisher-tools-unity-asset-store/
☄ Video: https://www.youtube.com/watch?v=RQ2fxox_JkU
Answer by foxtrot117 · May 06, 2020 at 05:46 PM
if you just want to use it with EditorGUILayout handling position you can do it like that:
GUILayout.Box (spriteRef.texture);
Your answer
![](https://koobas.hobune.stream/wayback/20220613193856im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer
Set MinWidth for EditorWindow 1 Answer
Tell difference between Undo & Redo on callback 0 Answers
Highlighting multiple sprites automatically changes all their materials in Unity 5?! 1 Answer
Editor Window Views 0 Answers