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 :
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;
}
}