Using a Texture as a Button

I just spent the last two hours searching for different ways to implement textures as buttons. There are many, many different ways of doing it. The solution I choose is not on these forums (or at least I couldn’t find it), so I’m posting it here:

Summary:
Create a GUI.Button that will appear as just a texture. This is accomplished by stripping the clear border that normally appears around buttons.

Steps to implement:

  • Create a GUISkin, then find the Button section. Set all values in the following sections to zero: Border, Padding, and Margin.
  • In OnGUI(), set the GUI.skin as the skin you created. ((OR, if needed, you can set the style in the individual buttons.))
  • Create a GUI.Button with this definition from the GUI.Button Scripting Reference:
    static function Button (position : Rect, image : Texture, style : GUIStyle) : bool

Thanks, this helped me!

But how do you reference an image in your library as a texture in the button command? If I type the name of the image it treats it as a string.

textures are not referenced by strings but by Texture2D objects.

I know a string doesn’t work, I just said that.

The question is how to reference my texture.

Create a public GUIStyle object in your class.

In your Button code, use the call that lets you specify a GUIStyle.

Then, in the inspector, open up your object, your GUIStyle object should be there. Open it up and in Normal and Hover there will be slots to drag your textures into.

Awesome. Thanks!