Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Paulius-Liekis · Apr 03, 2013 at 09:23 AM · editorinspectorcolorskin

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image d.navarro · Apr 10, 2015 at 12:32 PM 0
Share

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 :-/

avatar image KdRWaylander · Apr 10, 2015 at 02:47 PM 0
Share

$$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 ^^

avatar image Paulius-Liekis · Apr 10, 2015 at 03:31 PM 0
Share

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

avatar image numberkruncher · Apr 10, 2015 at 07:41 PM 1
Share

@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 Replies

· Add your reply
  • Sort: 
avatar image
3

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);
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image d.navarro · Apr 12, 2015 at 07:12 AM 0
Share

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

avatar image numberkruncher · Apr 12, 2015 at 08:42 AM 1
Share

@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).

avatar image d.navarro · Apr 12, 2015 at 09:18 AM 0
Share

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

avatar image Deadcow_ · May 16, 2015 at 01:14 PM 0
Share

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?

avatar image Fydar · May 27, 2017 at 02:58 PM 0
Share

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.

avatar image
2

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deadcow_ · May 16, 2015 at 12:20 PM 0
Share

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

avatar image Bunny83 · May 17, 2015 at 10:44 AM 1
Share

@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.

avatar image Deadcow_ · May 17, 2015 at 11:29 AM 0
Share

Thanks you! Very informative

avatar image
-1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image numberkruncher · May 27, 2017 at 04:08 PM 0
Share

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);
 }

avatar image Fydar · May 30, 2017 at 03:25 PM 0
Share

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.

avatar image
0

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;
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to assign a color shown in the "Color" window to a color field in the inspector? 6 Answers

GameObject position not matching up to its position in the inspector even though they are clearly linked [answered] 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges