On Editor GUI, How can I draw a 9 sliced image

Hello.

I am working on a Node editor using the unity editor gui functions. Id like to add images so my nodes look better, but a node of mine can expand in size, as such, Id like to have a 9 sliced image, where the corners stay the same but the center expands.

I have been messing with GUI.DrawTexture and GUI.DrawTextureWithTexCoords with different values but cant seem to achieve what I want, not even close, so, 9 sliced image would definately be the way to go.

How can one achieve that?

Thank you.

You should use the image as background image for a GUIStyle. The GUIStyle is the “visual heart” of the GUI system. It has a border field which is a RectOffset. It allows you to specify the fixed pixels from each edge.

The easiest way is to use a GUISkin asset to setup all your styles, however it’s also possible to create a GUIStyle at runtime but it has to be done inside OnGUI. You can’t create a new GUIStyle in Start / Awake.

You might have a look at my GUI crash course. I didn’t mention the border field there, but you should get a better understanding of the whole system. I just added some more information about the different stylestates.