Editor button with texture?

I’m working on an editor thing, and I wanted to use images for the buttons.

But I’m not sure how to access textures from the script. Since it’s an editor script, I can’t drop the texture on the script.

I’ve been looking around and it seems I have to use the AssetDatabase, but it’s a little confusing.

Why not use a Texture2D and reference it like you would from a GUIButton?

I used this to load images (Texture) :

Resources.Load("imagename") as Texture;

and images are in Resources/ folder

private Rect _rect = new Rect(10, 10, 100, 40);
private GUIContent _content;

void OnGUI() {
	if (null == _content)
		_content = new GUIContent("Click me", (Texture)Resources.Load("your_image"); // file name in the resources folder without the (.png) extension
	GUILayout.Button(_rect, _content);
}

Old thread but I just had a lot of trouble with this and got it working.

// Define a texture and GUIContent
private Texture button_tex;
private GUIContent button_tex_con;
...
// Load the texture resource
this.button_tex = (Texture)AssetDatabase.LoadAssetAtPath("Assets/folder/folder/myButton.png", typeof(Texture));

// Define a GUIContent which uses the texture
this.button_tex_con = new GUIContent(button_tex);
...
// Use it in a button, this is sumple formatting for example
GUILayout.Button(button_tex_con, GUILayout.Width(100), GUILayout.Height(100))
12 Likes