0
\$\begingroup\$

When setting the color i'm not using the alpha because then it will not change the color at all and it will be transparent. in this line :

currentStyle.normal.background = MakeTex(2, 2,
                        new Color(color.a, color.b, color.g));

The result when changing the color in the inspector in run time : The color of the PAUSE button on the left is not the same color i changed it to on the right in the inspector :

Not same colors

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

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

    private GUIStyle currentStyle = null;
    private Color oldColor;

    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;
        if (GUI.Button(new Rect(10, 50, 170, 30), "CHANGE DIRECTION"))
            Debug.Log("Clicked the button with text");

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

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

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

        
    }

    private void InitStyles()
    {
            currentStyle = null;

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

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

                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
  • \$\begingroup\$ You might want to know that the way you are building UIs here is very much obsolete. The class GUI was made obsolete by the canvas-based UI system, and that system is now itself in the process of being made obsolete by the web-inspired UIToolkit. \$\endgroup\$
    – Philipp
    Apr 20, 2022 at 13:05

1 Answer 1

2
\$\begingroup\$

Here:

new Color(color.a, color.b, color.g)

Do you mean:

new Color(color.r, color.g, color.b)
\$\endgroup\$

You must log in to answer this question.

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