Search Unity

Question How Do You Change A Button's Background Color in an EditorWindow?

Discussion in 'Scripting' started by JustThisGuy, Jan 25, 2022.

  1. JustThisGuy

    JustThisGuy

    Joined:
    Mar 12, 2014
    Posts:
    7
    Hi All,

    I'm trying to change a button's background color in an EditorWindow. I can change tint using GUI.backgroundColor which seems to AND the color with grey, but I want to change it to an exact color. This is what I'm seeing:
    2022-01-25 11_03_10-Button Background Color.png
    Here's the code:

    EditorWindowButtonBackgroundColor.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class EditorWindowButtonBackgroundColor : EditorWindow
    5. {
    6.   [MenuItem("Tools/Button Background Color Editor Window")]
    7.   private static void OpenWindow()
    8.   {
    9.     GetWindow<EditorWindowButtonBackgroundColor>("Button Background Color");
    10.   }
    11.  
    12.   public void OnGUI()
    13.   {
    14.     GUIStyle yellowBackgroundStyle = new GUIStyle(GUI.skin.button);
    15.  
    16.     yellowBackgroundStyle.normal.background = MakeBackgroundTexture(10, 10, Color.yellow);
    17.  
    18.     if (GUILayout.Button("GUIStyle Button", yellowBackgroundStyle))
    19.     {
    20.       Debug.Log("GUIStyle Button");
    21.     }
    22.  
    23.     Color originalBackgroundColor = GUI.backgroundColor;
    24.  
    25.     GUI.backgroundColor = Color.yellow;
    26.  
    27.     if (GUILayout.Button("GUI.backgroundColor Button", yellowBackgroundStyle))
    28.     {
    29.       Debug.Log("GUI.backgroundColor Button");
    30.     }
    31.  
    32.     GUI.backgroundColor = originalBackgroundColor;
    33.   }
    34.  
    35.   private Texture2D MakeBackgroundTexture(int width, int height, Color color)
    36.   {
    37.     Color[] pixels = new Color[width * height];
    38.  
    39.     for (int i = 0; i < pixels.Length; i++)
    40.     {
    41.       pixels[i] = color;
    42.     }
    43.  
    44.     Texture2D backgroundTexture = new Texture2D(width, height);
    45.  
    46.     backgroundTexture.SetPixels(pixels);
    47.     backgroundTexture.Apply();
    48.  
    49.     return backgroundTexture;
    50.   }
    51. }
    I got the code for MakeBackgroundTexture here:
    https://forum.unity.com/threads/giving-unitygui-elements-a-background-color.20510/

    As you can see, modifying and applying a GUIStyle is not working for me. Any idea what I'm doing wrong?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @JustThisGuy

    Would something like this work?

    And which version of Unity are you using?

    This is with 2019.4:
    upload_2022-1-25_23-29-39.png

    I wouldn't use that button style you have. It is possible to get the above look with this kind of setup:
    Code (CSharp):
    1. void OnEnable() =>
    2.     tex = MakeBackgroundTexture(1, 1, new Color32(255, 255, 0, 255));
    3.  
    4. void OnGUI()
    5. {
    6.     // Flat color
    7.     GUIStyle style = new GUIStyle();
    8.     style.normal.background = tex;
    9.     style.margin = new RectOffset(4,4,2,2);
    10.     style.alignment = TextAnchor.MiddleCenter;
    11.  
    12.     if (GUILayout.Button("Flat button", style))
    13.         Debug.Log("GUIStyle Button");
    14.  
    15.  
    16.     // Slightly tinted color
    17.     // Change GUI color
    18.     // Don't use button style
    19.     GUI.color = new Color32(255, 255, 0, 255);
    20.  
    21.     if (GUILayout.Button("Tinted Button"))
    22.         Debug.Log("Tinted Button");
    23. }
    The first one is exact color, the second is not, but it looks like a button. For the second one, you'll have to also reset the GUI color afterwards. IIRC there are some other ways too but unfortunately I can't remember.
     
    Last edited: Jan 25, 2022
    JustThisGuy likes this.
  3. JustThisGuy

    JustThisGuy

    Joined:
    Mar 12, 2014
    Posts:
    7
    I'm using 2020.3.22f1.

    So it looks like you can't change the texture when basing it off of an existing style. Thanks for the info! Seems strange that it just silently fails though.
     
    eses likes this.