Search Unity

Change GUI UI Background Color

Discussion in 'Scripting' started by mholmes, Oct 20, 2018.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    I'm a newb to GUI UI, trying to change the Background color and something is not working:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using WBG;
    6.  
    7. public class btnStoreButtonClick : MonoBehaviour
    8. {
    9.     private Rect windowRect = new Rect((Screen.width - 400) / 2, (Screen.height - 200) / 2, 400, 200);
    10.     private bool show = false;
    11.     private string _Title;
    12.     private string _Message;
    13.     Color _WBGColor = new Color(91f, 203f, 188f, 10f);
    14.  
    15.     void OnGUI()
    16.     {
    17.         GUI.backgroundColor = _WBGColor;
    18.         if (show) { windowRect = GUI.Window(4, windowRect, DialogWindow, _Title); }
    19.     }
    20.  
    21.     // This is the actual window.
    22.     void DialogWindow(int windowID)
    23.     {
    24.         GUI.Label(new Rect(5, 20, windowRect.width, 20), _Message);
    25.  
    26.         switch (windowID)
    27.         {
    28.             case 0:
    29.                 if (GUI.Button(new Rect(320, 160, 50, 30), "OK"))
    30.                 {
    31.                     show = false;
    32.                 }
    33.                 break;
    34.             case 1:
    35.                 if(GUI.Button(new Rect(200, 160, 50,30), "Abort"))
    36.                 {
    37.                     show = false;
    38.                 }
    39.                 else if(GUI.Button(new Rect(260, 160, 50, 30), "Cancel"))
    40.                 {
    41.                     show = false;
    42.                 }
    43.                 else if (GUI.Button(new Rect(320, 160, 50, 30), "OK"))
    44.                 {
    45.                     show = false;
    46.                 }
    47.                     break;
    48.             case 2:
    49.                 if (GUI.Button(new Rect(270, 160, 50, 30), "Yes"))
    50.                 {
    51.                     show = false;
    52.                 }
    53.                 else if (GUI.Button(new Rect(330, 160, 50, 30), "No"))
    54.                 {
    55.                     show = false;
    56.                 }
    57.                 break;
    58.             case 3:
    59.                 if (GUI.Button(new Rect(270, 160, 50, 30), "Retry"))
    60.                 {
    61.                     show = false;
    62.                 }
    63.                 else if (GUI.Button(new Rect(330, 160, 50, 30), "Cancel"))
    64.                 {
    65.                     show = false;
    66.                 }
    67.                 break;
    68.             case 4:
    69.                 if (GUI.Button(new Rect(270, 160, 50, 30), "Help"))
    70.                 {
    71.                     show = false;
    72.                 }
    73.                 else if (GUI.Button(new Rect(330, 160, 50, 30), "OK"))
    74.                 {
    75.                     show = false;
    76.                 }
    77.                 break;
    78.             default:
    79.                 if (GUI.Button(new Rect(270, 160, 50, 30), "Yes"))
    80.                 {
    81.                     show = false;
    82.                 }
    83.                 else if(GUI.Button(new Rect(330, 160, 50, 30), "No"))
    84.                 {
    85.                     show = false;
    86.                 }
    87.                 break;
    88.         }
    89.     }
    90.  
    91.  
    92.     public void ShowMessageBox(string Title, string Message)
    93.     {
    94.         _Title = Title;
    95.         _Message = Message;
    96.         show = true;
    97.     }
    98. }
    99.  

    I thought this was how you built color via rgb:
    Code (CSharp):
    1. Color _WBGColor = new Color(91f, 203f, 188f, 10f);
     
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    No, Color is a float from 0-1 Color32 is a byte from 0-255.
     
  3. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    So use color32 instead of color to set the background color? Tried this but now the message box does not popup:
    Code (CSharp):
    1. Color32 _WBGColor = new Color32(91, 203, 188, 10);
    Never mind I got it working now. How do you set the color for the rect and the title bar area?
     
    Last edited: Oct 20, 2018
  4. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    Are you sure that it's not showing up? You have an alpha of 10 which is very low.
     
  5. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Hmm ok ill adjust the alpha and try again.