Hello. I was wondering if any of you knew how to change the color of a GUI.Box. I already have a code with GUI.Box in it and all I want to do is make one of them white and one of them green. And if you know how, can you also please tell me how to make one of my boxes change from green to red smoothly? What I mean by smoothly is that it should go from green to light green to yellow to orange to red, if thats possible. Thanks in advance!
Here’s my code:
var barDisplay : float = 0;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;
function OnGUI()
{
GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
}
function Update()
{
barDisplay = Time.time * 0.05;
}
You might want to use a white background texture for your button (defined in your GUIStyle), and then GUI.backgroundColor will tint your button the way you would expect:
void OnGUI() {
GUI.backgroundColor = Color.red;
GUI.Button(new Rect(10, 10, 100, 30), "Red button", _yourGUIStyle); // your style with white background texture
}
If you’re like me and just want to create an empty box use this. Avoids memory leak issues caused by creating textures on the fly (common problem with Unity Editor tools).
void DrawBox (Rect position, Color color) {
Color oldColor = GUI.color;
GUI.color = color;
GUI.Box(position, "");
GUI.color = oldColor;
}
Maybe I shouldn’t have assumed it was editor code (since thats what i work with)
But even then, the only reason it doesn’t work with the default game skin is because the box is black. Change box.normal.background to anything else and GUI.color works fine
(did anyone notice that you can not define own colors but only use the static Color definitions like Color.green or Color.yellow ? If you use a Color like Color new Color(120f,40f,200f,255f) it defaults to white. I guess this is wanted behaviour, but hmm.)
var background = GUI.skin.box.normal.background;
GUI.skin.box.normal.background = Texture2D.grayTexture;
// bla bla
GUI.skin.box.normal.background = background;
UPD
Most likely you need to cache the Texture2D.grayTexture somewhere