0
\$\begingroup\$

This is just a test.

The PING PONG button color is in Red and also the START/STOP button start with the Red color. but in the START/STOP button i'm trying to keep the original color and then to change the color. The problem is that the content of the START/STOP button color is changed to red too and i want to keep the content color of the START/STOP to be white like in the PING PONG button.

In this screenshot the LOOP and CHANGE DIRECTION buttons are in the original colors. The PING PONG is in red color and original content(white). The STOP button is what i'm trying to to be able to change it's background color with keeping the original background settings but the content color is not white.

Colors

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GUIExamples : MonoBehaviour
{
    public Texture btnTexture;
    public Color color;
    public GUISkin skings;
    
    public string colorInf;

    private GUIStyle currentStyle = null;
    private Color oldColor;
    private bool test = false;
    private string test1;

    private void Start()
    {
        oldColor = color;

        
    }

    private void OnGUI()
    {
        if (!btnTexture)
        {
            Debug.LogError("Please assign a texture on the inspector");
            return;
        }

        //GUI.backgroundColor = color;
        if (GUI.Button(new Rect(10, 10, 170, 30), "LOOP"))
            Debug.Log("Clicked the button with an image");

        //GUI.backgroundColor = Color.green;
        if (GUI.Button(new Rect(10, 50, 170, 30), "CHANGE DIRECTION"))
            Debug.Log("Clicked the button with text");

        GUI.backgroundColor = Color.red;
        if (GUI.Button(new Rect(10, 90, 170, 30), "PING PONG"))
            Debug.Log("Clicked the button with text");

        var defaultColor = GUI.color;
        if (test == false)
        {
            GUI.color = Color.red;
            test1 = "STOP";
        }
        else
        {
            GUI.color = Color.green;
            test1 = "PLAY";
        }
        if (GUI.Button(new Rect(10, 130, 170, 30), test1))
        {
            Debug.Log("Clicked the button with text");
            test = !test;
        }
        GUI.color = defaultColor;

        InitStyles();
        GUI.Box(new Rect(10, 170, 170, 30), "PAUSE", currentStyle);
        if (GUI.Button(new Rect(10, 170, 170, 30), "PAUSE", currentStyle))
            Debug.Log("Clicked the button with text");
    }

    private void InitStyles()
    {
        if (oldColor != color)
        {
            currentStyle = null;
        }

        if (currentStyle == null)
        {
            currentStyle = new GUIStyle(GUI.skin.box);
            currentStyle.normal.background = MakeTex(2,2,
                new Color(color.r, color.g, color.b));

            oldColor = color;
        }
    }

    private Texture2D MakeTex(int width, int height, Color col)
    {
        Color[] pix = new Color[width * height];
        for (int i = 0; i < pix.Length; ++i)
        {
            pix[i] = col;
        }
        Texture2D result = new Texture2D(width, height);
        result.SetPixels(pix);
        result.Apply();
        return result;
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Check GUI API here.

backgroundColor Global tinting color for all background elements rendered by the GUI.

color Applies a global tint to the GUI. The tint affects backgrounds and text colors.

contentColor Tinting color for all text rendered by the GUI.

In IMGUI, It retains many old GUI drawing modes(this reminds me of java awt or something like that).

Here's a hint:

Think of these variables as brushes. When you change the brush color, it affects all subsequent paints until it is set to another value. So restore the color after each drawing to keep the brush clean.

If you want to set the background color only. You can try this:

    var defaultColor = GUI.backgroundColor;
    if (test == false)
    {
        GUI.backgroundColor = Color.red;
        test1 = "STOP";
    }
    else
    {
        GUI.backgroundColor = Color.green;
        test1 = "PLAY";
    }
    if (GUI.Button(new Rect(10, 130, 170, 30), test1))
    {
        Debug.Log("Clicked the button with text");
        test = !test;
    }
    GUI.backgroundColor = defaultColor;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .