'Unity - Set GUI.Box background color

I'm trying to set the background color of a GUI.Box:

void OnGUI()
        {
            string LatLong;
           LatLong = map.calc.prettyCurrentLatLon;
            var mousePosition = Input.mousePosition;
            float x = mousePosition.x + 10;
            float y = Screen.height - mousePosition.y + 10;
            GUI.backgroundColor = Color.red;
            GUI.Box(new Rect(x, y, 200, 200), LatLong);
        }

However, the box is showing in a semi-transparent black, and the white text is subdued, not opaque white.

enter image description here



Solution 1:[1]

I'm gonna slide in with a more elegant solution here before this question gets old. I saw Thomas's answer and started to wonder if there is a way to do that without having to do the "InitStyles" in the OnGUI loop. Since ideally you only want to init the GuiSkin once in Awake or Start or wherever, but only once, and then never check to see if it's null ever again.

Anyway, after some trial and error, I came up with this.

private void Awake() {
    // this variable is stored in the class
    // 1 pixel image, only 1 color to set
    consoleBackground = new Texture2D(1, 1, TextureFormat.RGBAFloat, false); 
    consoleBackground.SetPixel(0, 0, new Color(1, 1, 1, 0.25f));
    consoleBackground.Apply(); // not sure if this is necessary

    // basically just create a copy of the "none style"
    // and then change the properties as desired
    debugStyle = new GUIStyle(GUIStyle.none); 
    debugStyle.fontSize = 24;
    debugStyle.normal.textColor = Color.white;
    debugStyle.normal.background = consoleBackground;
}

Solution 2:[2]

There's a better way to do all this that I came to after working with both solutions by Andrew900460 and Thomas. I believe you shouldn't always create new styles and instead create the styles as well as background textures when necessary on first load or regenerate them if one of the styles doesn't exist.

When you open the editor window, you want it to check if files exist for styles and their corresponding texture, if they don't then you'll want the editor to automatically create then save them to both arrays and as separate files.

Create the textures in a Texture2D array and save each texture after generation to a PNG file using EncodeToPNG() then writing the file to a location you can load it from. Then create the styles and save them as JSON files using JSONUtility. Use a texture atlas to link textures as backgrounds for normal, active and hover states every time you reload the GUIStyle file as sometimes they can no longer link back when being loaded in due to Unity being loaded up just recently or being unloaded.

Unless you're actively expanding the amount of textures and styles you're using, only do this file generation process once and preferably have a JSON file along the lines of a style manifest so the editor can automatically save and load all generated style files from their names or asset GlobalObjectIds. Once all the generation is done, you should have a stored GUIStyle[] array which will let you fetch any style you wish, though preferably you may want a custom struct which has a string for a name and a GUIStyle for the contents.

You'll always need to check if at least one of the styles goes NULL or otherwise unloads so the custom editor window can reload all style files and textures again. They go null because you either no longer have focus on Unity, have left the engine open and idle too long for the memory to be cleared or compressed or otherwise have caused a scenario where Unity no longer sees the style data as required. This isn't an inefficient or laggy thing you need to do, you can just check if the first style name is equal to null or if using a style retrieval method such as InsertCustomStyleLoader.GetStyle(styleName) returns a temporarily blank or null GUIStyle.

The reason I came to this was I'm actively making my own plugins and needed a more reliable, efficient way to load GUIStyles without having to rely on instructions going to waste loading assets that should already be created unless othewise said to. I'll come back when my current in-dev plugin (Obscura) is open-source with a link to the style-loading method directly so future people with curiosity in editor windows can tinker away freely using it.

Andrew and Thomas were both right, to an extent. They were just reliant on the style being constantly recreated every editor window update (not efficient) or not unloading at all (very volatile and frequently breaks, resulting in a NullReferenceException flooding the console).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew900460
Solution 2 neopolitans