Hi,
I am currently working on a little Tile-based Map Editor, using unity sprites.
The problem right now is, that I don’t know the best way to render a single sprite (not the whole sheet) inside an editor window.
What I currently do is, that I take the sprites texture, and render a part of it into a new Texture2D
, using the GetPixels
function and the sprites textureRect
. However, to do this inside a Script, the texture needs to have some specific import settings for me to be allowed to read data from it. (Where do I set the import properties?)
Also I would like to know how I add a click event to this sprite.
Hi !
You can render a Sprite into EditorGUI using…
GUI.DrawTextureWithTexCoords or Graphics.DrawTexture
Here’s some code from one of my project.
I’m using thoses functions to display my sprites on my own EditorWindow
public static void DrawTexture(Rect position, Sprite sprite, Vector2 size)
{
Rect spriteRect = new Rect(sprite.rect.x / sprite.texture.width, sprite.rect.y / sprite.texture.height,
sprite.rect.width / sprite.texture.width, sprite.rect.height / sprite.texture.height);
Vector2 actualSize = size;
actualSize.y *= (sprite.rect.height / sprite.rect.width);
Graphics.DrawTexture(new Rect(position.x, position.y + (size.y - actualSize.y) / 2, actualSize.x, actualSize.y), sprite.texture, spriteRect, 0, 0, 0, 0);
}
public static void DrawTextureGUI(Rect position, Sprite sprite, Vector2 size)
{
Rect spriteRect = new Rect(sprite.rect.x / sprite.texture.width, sprite.rect.y / sprite.texture.height,
sprite.rect.width / sprite.texture.width, sprite.rect.height / sprite.texture.height);
Vector2 actualSize = size;
actualSize.y *= (sprite.rect.height / sprite.rect.width);
GUI.DrawTextureWithTexCoords(new Rect(position.x, position.y + (size.y - actualSize.y) / 2, actualSize.x, actualSize.y), sprite.texture, spriteRect);
}
Hope it’s help !
I don’t quite understand the problem, but setting the sprite sheet’s mode to multiple and then in the Sprite Editor, you slice them using the automatic slicer on the top left. Does this help?
Header Image with Click Event (Open Link) 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.
Documentation.
Video: