Non-transparent GUI control background?

Does anyone know how to make the background of the standard box GUI control non-transparent?

From what I can tell, the box GUI control background is an ARGB32 formatted Texture2 object, but I can’t work out how to stop it’s alpha channel from being used. Does anyone know if it’s possible to stop the alpha channel being used? And if so, how?

If the alpha channel can’t be disabled, is there any other way I can get a solid background without having to resort to creating a new skin?

Thanks in advance.

the short answer is feed it a non-transparent image. which means you will need to create a new gui skin… even if all you change is the box background image.

Cheers.

If you just want to change the box, you can make a new GUIStyle… something like this:

var myBox : GUIStyle;

function OnGUI () {
  GUI.Box (Rect (10,10,100,100), "Hi there", myBox);
}

This will give you a single style you can just set from the inspector

Thanks for the replies/suggestions.

I’ve managed to work out how to temporarily override the default GUI texture with a non-transparent texture thereby eliminating the need for a custom GUIStyle.

can you share the solution? so people in the future will have a valid reference if they have a similar problem.

Cheers.

Sure, use the following steps:

  1. In a paint program, create a 12 x 12 pixel texture in the colour you want the background to be. Chamfer 3 pixels off each corner to give them a rounded look. Place a single pixel black border around the texture to make it look like the standard GUI element background. Don’t assign an alpha map or layer to the texture.

  2. assign the non-transparent background texture (created in step 1) to a Texture2D typed variable (called ‘nonTransparentTexture’ in the example code below).

  3. Use the following code inside the OnGUI() function to temporarily override the default texture:

// Save the default GUI background texture. This could be done outside OnGUI().
var defaultGUIBackgroundTexture : Texture2D = GUI.skin.box.normal.background;

// Override the default texture with the custom texture.
GUI.skin.box.normal.background = nonTransparentTexture;

// Display a box with the non-transparent background
GUI.Box (Rect ( x, y, width, height), "box text");  

// Restore the default transparent GUI background
GUI.skin.box.normal.background = defaultGUIBackgroundTexture;

Note: the ‘box.normal’ reference in the GUI calls in the above code will need to be changed to whatever GUI component style and state you are wanting to override. For example, using ‘GUI.skin.button.hover.background’ will override the background texture of the Button control when the mouse is hovering over it.

Thanks for that. I learnt something from it myself. Nice solution.

Cheers.

You’re welcome, and thanks for the compliment :slight_smile: