Is there a way to get Editor background color?

Is there a way to get Editor background color? I want to match color of my component to color of the Editor skin. Sure, I could hardcode dark/light skin colors, but maybe there is more elegant way?

1 Like

I would like to bump this question. I also need this, and I haven't found a way of getting the background color for an Editor Window. Hard-cording dark/light skin colors does not work, because it doesn't have Editor>Preferences>Colors>PlaymodeTint into consideration. I also don't know a way of getting the PlaymodeTint from script :-/

–

Mmmm, take a screenshot, open it in paint, pick the color with the color selector tool and then open the color panel so you can see its RGB code ? ^^ Well, that's how i'd do ^^

–

Problems is that we want to make some tool/dialog and match background color. How do you know that user has light/dark skin?

–

@dnavarro If you are drawing your GUI in the standard way with GUIStyle's and GUI.color then the entire GUI is tinted automatically.

–

4 Answers

4

There doesn’t seem to be an API for accessing the current background color (which agreed is a little annoying) but something like the following should work:

Color backgroundColor = EditorGUIUtility.isProSkin
    ? new Color32(56, 56, 56, 255)
    : new Color32(194, 194, 194, 255);

This works in edit mode, but fails in play mode, as it doesn't have into account the Playmode Tint.

–

@dnavarro If you are using GUI.DrawTexture or GUIStyle.Draw in your GUI then the proper tint will be applied. You can use this with the white pixel texture EditorGUIUtility.whiteTexture. Draw the white texture and set GUI.color or GUI.contentColor to the background color (depending upon which drawing method you are using).

–

Ah great, thank you numberkruncher :-) I'll try that tomorrow!

–

I use GUI.DrawTexture with GUI.color, but color not affected by playmode tint. Actual code: var defColor = GUI.color; GUI.color = EditorGUIUtility.isProSkin ? (Color)new Color32(56, 56, 56, 255) : (Color)new Color32(194, 194, 194, 255); GUI.DrawTexture(selectionRect, EditorGUIUtility.whiteTexture); GUI.color = defColor; What I do wrong? Or how to use GUIStyle to draw coloured empty texture?

–

Actually there is an API to get the GUISkin used by the editor: EditorGUIUtility.GetBuiltinSkin

This for example would return the GUISkin used by the inspector:

GUISkin skin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector);

See also: EditorSkin, EditorStyles

Can you please tell how to get background color from GuiSkin?

–

Thanks you! Very informative

–

If you feel like not using some hardcoded values, here is a different approach. EditorGUIUtility has a private method that tells us the actual colors. I presume the method is used internally across the Unity Editor.

private static Color GetDefaultBackgroundColor()
{
	float kViewBackgroundIntensity = isProSkin ? 0.22f : 0.76f;
	return new Color(kViewBackgroundIntensity, kViewBackgroundIntensity, kViewBackgroundIntensity, 1f);
}

Some Reflection magic should happen to be able to use it. Note that non-public methods are supposed to be used by Unity internally. So these methods may change in future Unity releases without any notification. Though the need for getting the background color is so fundamental that I don’t think it will ever be renamed or removed. So I feel like calling it via reflection would not going to be a problem ever IMHO.

Here is the code. Reflection calls are heavy on the processor. So the private method is called only once and the color is cached for further use.

private static Color _DefaultBackgroundColor;
public static Color DefaultBackgroundColor
{
	get
	{
		if (_DefaultBackgroundColor.a == 0)
		{
			var method = typeof(EditorGUIUtility)
				.GetMethod("GetDefaultBackgroundColor", BindingFlags.NonPublic | BindingFlags.Static);
			_DefaultBackgroundColor = (Color)method.Invoke(null, null);
		}
		return _DefaultBackgroundColor;
	}
}

Try this:

private static void DrawEmpty (Rect rect) {
		Color col = EditorGUIUtility.isProSkin
			? (Color) new Color32 (56, 56, 56, 255)
			: (Color) new Color32 (194, 194, 194, 255);
		
		Texture2D tex = new Texture2D (1, 1);
		tex.SetPixel (0, 0, col);
		tex.Apply ();
		GUI.DrawTexture(rect, tex);

	}

It draws a box the same colour as the editor background.