How to stretch a texture on a button?

Hi,

I’m trying to stretch my buttons to fit on any resolution. According to the API, this can be done easily with a GuiTexture, but not with a texture applied to a button?

 if(GUI.Button(Rect(xDistance,yDistance*3,buttonWidth,buttonHeight),optionsTexture, ScaleMode.ScaleToFit, true, 10.0F))
     {
     Application.LoadLevel(6);
     }

Any ideas why it won’t work?

Thanks a bunch!

  • i see too much arguments in GUI.Button method…
  • image that used as content (in your example) will not be scaled at all. if you use that image as button image, it should be part of GUI style (link below). this lets you use images as button background textures, customize non-scaling borders etc.

Try using ScaleMode.StretchToFill instead of ScaleMode.ScaleToFit.

I wrote this function as a drop-in replacement for GUI.Button(Rect,Texture2D):

	bool GUIButtonTexture2D( Rect r, Texture2D t)
	{
		GUI.DrawTexture( r, t);
		return GUI.Button( r, "", "");
	}

It should have the desired minify and magnify behaviors that the original poster seeks.

Obviously, you must only call it from inside your OnGUI() function.

Button scales textures down if needed, so make your texture big enough for your max resolution.

Scaling textures up makes them ugly anyway, so large enough texture is an easy (if not ideal) solution.