Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by sheld · Jun 23, 2020 at 12:48 PM · editorwindowguiskinstylebutton backround

Can't change button GUIStyle background.

Unity version: 2019.4.1f1

I'm trying to change background of button in my EditorWindow. Trying to achieve effect pressed button in normal state. alt text

 void OnGUI() {
     buttonStyleNormal = new GUIStyle(GUI.skin.button);
     buttonStyleToggled = new GUIStyle(GUI.skin.button);

     buttonStyleToggled.active.textColor = Color.blue;
     buttonStyleToggled.normal = buttonStyleToggled.active;

     ...

     GUILayout.Button("Developing", buttonStyleToggled);
     GUILayout.Button("Compilation", buttonStyleNormal);
 }

Result is here: alt text Problem stays even if I set background explicitly:

 buttonStyleToggled.normal.background = buttonStyleToggled.active.background;
 buttonStyleToggled.normal.scaledBackgrounds = buttonStyleToggled.active.scaledBackgrounds;
ss-2.png (10.2 kB)
ss-1.png (9.9 kB)
Comment
Add comment · Show 1
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 CodesCove · Jun 23, 2020 at 08:43 PM 0
Share

This might not solve your problem but it seems ok code to me.. and I also tested it with inspector and editorwindow and it worked ok (didn't need the explicit setting) .. I do have different Unity version (2018.4).. hope you get it working

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by frankslater · Jul 13, 2020 at 10:35 AM

Hey @sheld,


When I saw your question, I recalled talks about a new GUI on its way, and we could actually see the look of the editor change in recent versions, so I looked into it a little.

Here is what I found:



The problem


My guess is that this is down to the new GUI: UIElements.

Specifically how IMGUI is converted to UIElements for built-in Editor GUI.skin.


You may have noticed that backgrounds are null:

GUI.skin.button.normal.background == null
GUI.skin.button.active.background == null

So when you do buttonStyleToggled.normal.background = buttonStyleToggled.active.background;, it actually does nothing.

If you set the background to a Texture2D (that's not null), that gets applied.


I also found that there is a UIElements Debugger at Window > Analysis > UIElements Debugger. Looking at the UIElements Debugger, it looks like IMGUI is displayed through UIElements within an IMGUIContainer, and my guess is that all built-in Editor GUI.skin have UIElements style sheets (USS) and elements they are automatically converted to.


For example, if you do this

GUIStyle btn = new GUIStyle("Button");
btn.name = "customButton"; // from "button"
btn = new GUIStyle(btn);

your button background will be gone, so I'm guessing the name is used as the style class or something similar. So it's important to note, that changing name of styles where you want to use builtin backgrounds will mess up your and possibly other things.



Solutions


1. Use UIElements instead of IMGUI. If you are lucky, your project only needs to work in Unity versions that already supports it (I think 2019.1+), so you can leverage its full potential.


If your project needs to support older (IMGUI only) Unity versions, you can use preprocessor directives e.g.

 #if UNITY_2019_1_OR_NEWER
     // UIElements GUI code
 #else
     // IMGUI code
 #endif

but in this case you may be more limited in what you can do with UIElements if you want your users to not get confused when switching between the two. For this reason, I would recommend the two GUI to be fairly similar (and that's what limits you to what IMGUI can do).


Here is a short UIElements Editor GUI example: Customize the Unity Editor with UIElements


2. Create standard Toggle buttons. So for example, updating your code to this might yield better results

 void OnGUI() {
      buttonStyle = new GUIStyle(GUI.skin.button);
      // Change buttonStyle in accordance with any custom look you would like
      // your buttons to have
      toggleButtonStyle = new GUIStyle(buttonStyle);
      toggleButtonStyle.active.textColor = Color.blue;
      ...
      toggleOn = GUILayout.Toggle(toggleOn, "Toggle Button", toggleButtonStyle);
      GUILayout.Button("Normal Button", buttonStyleNormal);
  }

annotation-2020-07-13-112853.png (10.5 kB)
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

133 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 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 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 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 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 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 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 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 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 use the default skin in a custom stlye? 3 Answers

Style dynamically created GUI Text 0 Answers

GUIskin and c#, How? 1 Answer

How to add Background Images for Vertical GUILayouts in Unity3d Custom Editor Window 0 Answers

Why is my customSkin null? 0 Answers


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