- Home /
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?
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 :-/
$$anonymous$$mmm, 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.
Answer by numberkruncher · Apr 10, 2015 at 07:38 PM
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?
The GUI.color is the tinted colour, and your overwriting it @Deadcow_, ins$$anonymous$$d, try something like 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);
}
You might want to cache the texture ins$$anonymous$$d of creating every time, but it should work for you.
You helped me come to that answer, you were so close but no cigar.
Answer by Bunny83 · Apr 12, 2015 at 11:53 AM
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?
@deadcow: There is nothing like a background color. Unity uses textures for almost everything. Each GUIStyle that has some kind of background has a texture set to the background field of one of it's GUIStyleStates. The usual style-state you want to use is the normal state. See my answer over here for more details.
Answer by Fydar · May 27, 2017 at 03:00 PM
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.
It is very inefficient to allocate textures like this. I'd highly recommend caching your texture or simply using the built-in white texture with tinting.
Also it is worth noting that there are issues when drawing textures in this way in custom property drawers in the inspector window. A workaround to those issues is to use the GUIStyle
drawing mechanism which does not exhibit this poor behaviour.
For example,
private static GUIStyle s_TintableStyle;
private static GUIStyle TintableStyle {
get {
if (s_TintableStyle == null) {
s_TintableStyle = new GUIStyle();
s_TintableStyle.normal.background = EditorGUIUtility.whiteTexture;
s_TintableStyle.stretchWidth = true;
}
}
}
private static void DrawEmpty(Rect rect, Color color) {
// Only need to perform drawing during repaints!
if (Event.current.type == EventType.Repaint) {
var restoreColor = GUI.color;
GUI.color = color;
TintableStyle.Draw(rect, false, false, false, false);
GUI.color = restoreColor;
}
}
private static void DrawEmpty(Rect rect) {
var backgroundColor = EditorGUIUtility.isProSkin
? new Color32(56, 56, 56, 255)
: new Color32(194, 194, 194, 255);
DrawEmpty(rect, backgroundColor);
}
Thank you for finishing the code ;)
I made my one on mobile so I was in a rush xD
Normally to cache GUI styles I used the [UnityEditor.Callbacks.DidReloadScripts] attribute to generate the texture because it's faster, but it's not good if you're not actually using the style, but it reduces the need for 1 property call and 1 condition check.
private static GUIStyle TintableStyle;
[Callbacks.DidReloadScripts]
private static void OnReload () {
TintableStyle= new GUIStyle();
TintableStyle.normal.background = EditorGUIUtility.whiteTexture;
TintableStyle.stretchWidth = true;
}
private static void DrawEmpty(Rect rect, Color color) {
// Only need to perform drawing during repaints!
if (Event.current.type == EventType.Repaint) {
var restoreColor = GUI.color;
GUI.color = color;
TintableStyle.Draw(rect, false, false, false, false);
GUI.color = restoreColor;
}
}
private static void DrawEmpty(Rect rect) {
var backgroundColor = EditorGUIUtility.isProSkin
? new Color32(56, 56, 56, 255)
: new Color32(194, 194, 194, 255);
DrawEmpty(rect, backgroundColor);
}
But your answer is probably the better answer.
Answer by Can-Baycay · Apr 01, 2019 at 10:11 PM
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;
}
}
Your answer
Follow this Question
Related Questions
How to assign a color shown in the "Color" window to a color field in the inspector? 6 Answers
Insert new custom class element with _default_ values to a SerializedProperty array? 1 Answer
Custom Editor problems 2 Answers
Create inspector drop-down button based on the content of a list in editor mode 1 Answer