Editor scripting: Part of a texture as button content

Hello guys!

Since I want to get the hang of creating custom editor tools, I’ve been working on a tile based map editor.
It’s quite simple, and allows me to focus on creating editor tools.

I create a mesh out of quads, and assign a sprite sheet (or atlas if you will) as the main texture.
By switching around the UV coordinates of my vertices, I have a basic functioning tilemap.

I also have the basic controls down for creating this mesh through my editing window, and some simple tile picking code for in-editor tile changing.

Since I have all the information about what tiles are present in my main texture (sprite sheet/atlas), I would like to create a toggle button for each tile.
The user would be able to select a tile in the editor window (similar to the unity texture bowser), and “paint” it on the plane by mouse picking.

I’ve found different ways of drawing textures as controls or on controls, but in this particular case I need only a part of the texture.
I am aware that I could take the pixel data of my sheet, and create a new texture for each tile to use in my controls. While this could be done once,
when assigning a texture to my mesh, I figure it still creates a lot of overhead.

So I was wondering if I could utilize the same technique as when i draw on my quads: pass some UV coordinates. That way, only part of the texture will be rendered on my buttons.
The last solution i tried was reserving the spaces in the editor where my tiles should be by using GUILayoutUtility.GetRect(), saving these rects in a list, and later draw my texture using Graphics.DrawTexture(), which allows for UV’s as parameters.
This, in effect, draws only the needed tile on the correct position while maintaining my layout.
While this works, it goes against the GUILayout’s principle of assigning “content” to buttons, in fact, it doesn’t create buttons at all ^^. I’m kind of all over the place trying to make this work, and getting it right would give me much more insight in creating decent editor windows.

So what should be the best practice when creating a “grid” of buttons (similar to the unity texture bowser), each with a part of a texture as content?
Any thoughts on this? Or should I conclude that creating a seperate texture for each tile in advance is the best course of action?

Thanks in advance!

Try GUI.DrawTextureWithTexCoords().