Can I set a solid background for a GUI label?

I have a GUI Label in my program that comes up next to the mouse when it hovers over a certain collider.
The colour of the object attached to the collider changes, so I would like to ensure that the label’s text always has maximum visibility by setting a solid-coloured background for it. Is there a way to set a background texture or colour for labels?

The solution is to use a custom style that has a background texture for your label. Labels use the normal GUIStyleState, so that’s the one you want to modify. Warning; take care not to use Texture2D.blackTexture for your background, as it has an alpha of zero and will simply not show up.


private GUIStyle style;

void OnEnable()
{
	style = new GUIStyle();
	style.normal = new GUIStyleState();
	style.normal.textColor = Color.black;
	style.normal.background = Texture2D.whiteTexture;
}

Then, use the style as the third argument to Handles.Label, or other gui label functions, and you’ll have a much more readable in-scene label for your stuff.


void OnSceneGUI()
{
	// replace position with wherever you want to show the label
	Handles.Label(position, "some text", style);
}

Sure. Whatever script is using your OnGUI, reference an image. Then assign that image to the label. Just drag a texture to the image reference in the inspector on the script.


    [SerializeField] public Texture image;
    void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 100, 100), image, "Label");
    }