Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question How to change GUI.Button color at runtime in the inspector ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by MuhamadBanguzI1993, Apr 20, 2022.

  1. MuhamadBanguzI1993

    MuhamadBanguzI1993

    Joined:
    Apr 16, 2022
    Posts:
    3
    I'm trying to archive few things :

    1. To be able to change each GUI.Button color individual at runtime when changing each button color in the inspector change the color at real time.

    2. To make each button a color switch. if clicking a button once change the color and if clicking once again change to another color each time clicking the button change the color between two colors.

    In this screenshot i'm trying to do it with one button for start but it's not working.
    When making :

    Code (csharp):
    1.  
    2. GUI.backgroundColor = color;
    3.  
    The button background color become transparent like in this screenshot :


    The Search button background color is transparent.
    But if i'm doing for example :

    Code (csharp):
    1.  
    2. GUI.backgroundColor = Color.red;
    3.  
    Then the Search button background color will be red.

    But it's not working and very far from what i'm trying to archive.

    The full script :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class GUIExamples : MonoBehaviour
    7. {
    8.     public Texture btnTexture;
    9.     public Color color;
    10.  
    11.     private void OnGUI()
    12.     {
    13.         if (!btnTexture)
    14.         {
    15.             Debug.LogError("Please assign a texture on the inspector");
    16.             return;
    17.         }
    18.  
    19.         //GUI.backgroundColor = color;
    20.         if (GUI.Button(new Rect(10, 10, 170, 30), "LOOP"))
    21.             Debug.Log("Clicked the button with an image");
    22.  
    23.         //GUI.backgroundColor = color;
    24.         if (GUI.Button(new Rect(10, 50, 170, 30), "CHANGE DIRECTION"))
    25.             Debug.Log("Clicked the button with text");
    26.  
    27.         //GUI.backgroundColor = color;
    28.         if (GUI.Button(new Rect(10, 90, 170, 30), "PING PONG"))
    29.             Debug.Log("Clicked the button with text");
    30.  
    31.         //GUI.backgroundColor = color;
    32.         if (GUI.Button(new Rect(10, 130, 170, 30), "STOP"))
    33.             Debug.Log("Clicked the button with text");
    34.  
    35.         //GUI.backgroundColor = color;
    36.         if (GUI.Button(new Rect(10, 170, 170, 30), "PAUSE"))
    37.             Debug.Log("Clicked the button with text");
    38.  
    39.         ChangeColors();
    40.     }
    41.  
    42.     private void ChangeColors()
    43.     {
    44.         Color oldColor = GUI.backgroundColor;
    45.         GUI.backgroundColor = color;
    46.         GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
    47.         buttonStyle.fontSize = 18;
    48.  
    49.         bool searchButton = GUI.Button(new Rect(10, 210, 390, 30), "Search", buttonStyle);
    50.         if (searchButton)
    51.         {
    52.             // ...
    53.         }
    54.     }
    55. }
    56.  
    57.  
     

    Attached Files: