Search Unity

Change GUI.Box Color?

Discussion in 'Scripting' started by menachemt, Mar 19, 2013.

Thread Status:
Not open for further replies.
  1. menachemt

    menachemt

    Joined:
    Feb 15, 2013
    Posts:
    12
    Hello. I was wondering if any of you knew how to change the color of a GUI.Box. I already have a code with GUI.Box in it and all I want to do is make one of them white and one of them green. And if you know how, can you also please tell me how to make one of my boxes change from green to red smoothly? What I mean by smoothly is that it should go from green to light green to yellow to orange to red, if thats possible. Thanks in advance!
    Here's my code:
    Code (csharp):
    1.  
    2. var barDisplay : float = 0;
    3. var pos : Vector2 = new Vector2(20,40);
    4. var size : Vector2 = new Vector2(60,20);
    5. var progressBarEmpty : Texture2D;
    6. var progressBarFull : Texture2D;
    7.  
    8. function OnGUI()
    9. {
    10.  
    11.     GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
    12.         GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
    13.  
    14.         GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
    15.             GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
    16.         GUI.EndGroup ();
    17.  
    18.     GUI.EndGroup ();
    19.  
    20. }
    21.  
    22. function Update()
    23. {
    24.     barDisplay = Time.time * 0.05;
    25. }
    P.S. I take no credit for the code above, I found it here: http://answers.unity3d.com/questions/11892/how-would-you-make-an-energy-bar-loading-progress.html
     
  2. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    you can use the GUIStyle function..
     
  3. menachemt

    menachemt

    Joined:
    Feb 15, 2013
    Posts:
    12
    I can't. I tried and GUIStyle doesn't have background color. I kept getting errors. Any other way of doing it?
     
  4. Benderlab

    Benderlab

    Joined:
    Sep 18, 2012
    Posts:
    8
    Try this.

    Code (csharp):
    1.  
    2.  
    3. private GUIStyle currentStyle = null;
    4.  
    5. void OnGUI()
    6. {  
    7.     InitStyles();
    8.     GUI.Box( new Rect( 0, 0, 100, 100 ), "Hello", currentStyle );
    9. }
    10.  
    11. private void InitStyles()
    12. {
    13.     if( currentStyle == null )
    14.     {
    15.         currentStyle = new GUIStyle( GUI.skin.box );
    16.         currentStyle.normal.background = MakeTex( 2, 2, new Color( 0f, 1f, 0f, 0.5f ) );
    17.     }
    18. }
    19.  
    20. private Texture2D MakeTex( int width, int height, Color col )
    21. {
    22.     Color[] pix = new Color[width * height];
    23.     for( int i = 0; i < pix.Length; ++i )
    24.     {
    25.         pix[ i ] = col;
    26.     }
    27.     Texture2D result = new Texture2D( width, height );
    28.     result.SetPixels( pix );
    29.     result.Apply();
    30.     return result;
    31. }
    32.  
    33.  
     
  5. menachemt

    menachemt

    Joined:
    Feb 15, 2013
    Posts:
    12
    Hello. Thanks! This is what I wanted. :D
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  7. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    You might want to use a white background texture for your button (defined in your GUIStyle), and then GUI.backgroundColor will tint your button the way you would expect:

    Code (csharp):
    1. void OnGUI() {
    2.     GUI.backgroundColor = Color.red;
    3.     GUI.Button(new Rect(10, 10, 100, 30), "Red button", _yourGUIStyle); // your style with white background texture
    4. }
     
    Octoverse likes this.
  8. Ash-Blue

    Ash-Blue

    Joined:
    Aug 18, 2013
    Posts:
    102
    If you're like me and just want to create an empty box use this. Avoids memory leak issues caused by creating textures on the fly (common problem with Unity Editor tools).

    Code (CSharp):
    1.         void DrawBox (Rect position, Color color) {
    2.             Color oldColor = GUI.color;
    3.  
    4.             GUI.color = color;
    5.             GUI.Box(position, "");
    6.  
    7.             GUI.color = oldColor;
    8.         }
     
  9. bathingsoap

    bathingsoap

    Joined:
    Jun 9, 2016
    Posts:
    1
    JasonC_ likes this.
  10. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Well you see, you are only slightly correct


    Maybe I shouldn't have assumed it was editor code (since thats what i work with)
    But even then, the only reason it doesn't work with the default game skin is because the box is black. Change box.normal.background to anything else and GUI.color works fine

    Code (CSharp):
    1. public class Boxes : EditorWindow {
    2.     [MenuItem( "Window/Boxes" )]
    3.     static void Init () {
    4.         Boxes window = (Boxes) EditorWindow.GetWindow( typeof( Boxes ) );
    5.         window.Show();
    6.         window.position = new Rect( 20, 80, 310, 300 );
    7.     }
    8.     void OnGUI () {
    9.         GUILayout.Box( "Default Box" );
    10.         GUI.color = Color.green;
    11.         GUILayout.Box( "Green Box" );
    12.         GUI.color = Color.red;
    13.         GUILayout.Box( "Red Box" );
    14.     }
    15. }
     
    _watcher_ likes this.
  11. idbrii

    idbrii

    Joined:
    Aug 18, 2014
    Posts:
    51
    • GUI.color is great, but results in black text for Box.
    • You can change the text with a GUIStyle, but then GUI.color is ignored (nothing to tint).
    • You can add a texture to your GUIStyle, but then your text and background are tinted the same (can't have white text).
    • You can use GUI.backgroundColor instead of GUI.color, and then you're good!

    Code (CSharp):
    1. Awake() {
    2.         tintableText = new GUIStyle(GUI.skin.box);
    3.         tintableText.normal.background = Texture2D.whiteTexture; // must be white to tint properly
    4.         tintableText.normal.textColor = Color.white; // whatever you want
    5. }
    6.  
    7. OnGUI() {
    8.     var guicolor_backup = GUI.backgroundColor;
    9.     GUI.backgroundColor = Color.yellow * Color.grey;
    10.     GUILayout.Box("Hi", tintableText);
    11.     GUI.backgroundColor = guicolor_backup;
    12. }
     
    NeatWolf, vexe, Ash-Blue and 3 others like this.
  12. InsideTheKraken

    InsideTheKraken

    Joined:
    Aug 4, 2018
    Posts:
    1
    Hi @idbrii

    Quick question about this sample of code: how do you manage to call "GUI.skin.box" from outside the OnGUI() method?

    I get an error every time I try to do something like that...

    Thanks!
     
  13. TomTrottel

    TomTrottel

    Joined:
    Aug 16, 2014
    Posts:
    18
    Hi.

    (did anyone notice that you can not define own colors but only use the static Color definitions like Color.green or Color.yellow ? If you use a Color like Color new Color(120f,40f,200f,255f) it defaults to white. I guess this is wanted behaviour, but hmm.)
     
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Color uses 0.0 to 1.0 range, not 0 to 255 range.
     
    samousta and TomTrottel like this.
  15. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    480
    That worked for me:
    Code (CSharp):
    1. var background = GUI.skin.box.normal.background;
    2. GUI.skin.box.normal.background = Texture2D.grayTexture;
    3. // bla bla
    4. GUI.skin.box.normal.background = background;
    UPD
    Most likely you need to cache the `Texture2D.grayTexture` somewhere
     
  16. idbrii

    idbrii

    Joined:
    Aug 18, 2014
    Posts:
    51
    That's just pseudo code (it wouldn't compile because it's missing void too!).

    I usually do a null check in OnGUI to do my init:

    Code (CSharp):
    1. void InitGUI() {
    2.     tintableText = new GUIStyle(GUI.skin.box);
    3.     tintableText.normal.background = Texture2D.whiteTexture; // must be white to tint properly
    4.     tintableText.normal.textColor = Color.white; // whatever you want
    5. }
    6. void OnGUI() {
    7.     if (tintableText == null)
    8.     {
    9.         InitGUI();
    10.     }
    11.     var guicolor_backup = GUI.backgroundColor;
    12.     GUI.backgroundColor = Color.yellow * Color.grey;
    13.     GUILayout.Box("Hi", tintableText);
    14.     GUI.backgroundColor = guicolor_backup;
    15. }
    (untested again)
     
    spider853 likes this.
Thread Status:
Not open for further replies.