Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Non-transparent GUI control background?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Neural Echo, Nov 19, 2007.

  1. Neural Echo

    Neural Echo

    Joined:
    Jul 5, 2007
    Posts:
    83
    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.
     
  2. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    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.
     
  3. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    If you just want to change the box, you can make a new GUIStyle... something like this:

    Code (csharp):
    1.  
    2. var myBox : GUIStyle;
    3.  
    4. function OnGUI () {
    5.   GUI.Box (Rect (10,10,100,100), "Hi there", myBox);
    6. }
    7.  
    This will give you a single style you can just set from the inspector
     
  4. Neural Echo

    Neural Echo

    Joined:
    Jul 5, 2007
    Posts:
    83
    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.
     
  5. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    can you share the solution? so people in the future will have a valid reference if they have a similar problem.

    Cheers.
     
  6. Neural Echo

    Neural Echo

    Joined:
    Jul 5, 2007
    Posts:
    83
    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:

    Code (csharp):
    1. // Save the default GUI background texture. This could be done outside OnGUI().
    2. var defaultGUIBackgroundTexture : Texture2D = GUI.skin.box.normal.background;
    3.  
    4. // Override the default texture with the custom texture.
    5. GUI.skin.box.normal.background = nonTransparentTexture;
    6.  
    7. // Display a box with the non-transparent background
    8. GUI.Box (Rect ( x, y, width, height), "box text");  
    9.  
    10. // Restore the default transparent GUI background
    11. 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.
     
  7. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    Thanks for that. I learnt something from it myself. Nice solution.

    Cheers.
     
  8. Neural Echo

    Neural Echo

    Joined:
    Jul 5, 2007
    Posts:
    83
    You're welcome, and thanks for the compliment :)