custom icon for custom editor button?

Does anyone know how to get a custom icon for an editor button? I know GUILayout.Button takes a texture argument but I’m unsure how to get my own in.

If you want to setup the appearance from the editor you could setup a GUISkin and have some custom styles for your various buttons with custom images:

public GUISkin _editorSkin;
void OnGUI()
{
    GUIStyle style = this._editorSkin.GetStyle("YourCustomButton");
    if (GUI.Button(rect, content, style))
    {
        ...
    }
}

The texture argument for the button is just a way to provide an image instead of a text as the content of the button, if you mean to have the button display only an image you can do:

public Texture2D _buttonIcon;
void OnGUI()
{
    if (GUI.Button(rect, this._buttonIcon))
    {
        ...
    }
}

GUISkins are a whole part of unity I had no idea about. Thanks for introducing them to me. However I can’t quite figure out how to add any sort of texture or icon to them. I’ve found a different solution though I fear its not an ideal one.

Texture svicon = Resources.Load("saveicon") as Texture;

if( GUILayout.Button ( svicon, buttonStyle1 )  ){
//do stuff
}

Since I have to put my icon images in resources I fear they’ll probably go into the build when I build the game. They’re unnecessary for the game as this is an editor extension. Their file size is negligible but it still feels dirty.

If anyone can clarify putting textures in GUISkins or has a better solution I’d be elated to hear it.

You can load textures this way for anyone that still is interested:

Texture2D play = EditorGUIUtility.Load("Assets/Icons/play.png") as Texture2D;
5 Likes