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
1
Question by vexe · Jan 31, 2014 at 05:16 PM · guionguiguistyleoninspectorgui

How to use GUIStyles in OnInspectorGUI?

Hello - I'm trying to use a very simple GUIStyle for a label in my custom editor. However, when I use it I get all sorts of errors, ArgumentExceptions, NullReferneces, Can't use/call GUI stuff outside of OnGUI, etc.

How can I create a GUIStyle for my editor elements?

Here's what I'm doing:

 private readonly GUIStyle StatesLabel = new GUIStyle(GUI.skin.label)
 {
     alignment = TextAnchor.MiddleLeft,
     margin = new RectOffset(),
     padding = new RectOffset(),
     fontSize = 15,
     fontStyle = FontStyle.Bold
 };

Using it like:

 GUILayout.Label("States", StatesLabel);

Thanks for any help.

EDIT:

Alright it seems that the errors go away if I remove the GUI.skin.label from the GUIStyle constructor. - But if I do so, how can I then create styles for buttons, labels, toggles, etc?

It seems that I can't do anything "GUI"ish in OnInspectorGUI? (can't even do a GUI.backgroundColor change... - very frustrated)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Feb 01, 2014 at 02:13 AM

The problem is that GUI.skin, which is a property, can only be accessed inside OnGUI / OnInspectorGUI, like the error tells you. So you have to move the initialization to OnInspectorGUI:

 private bool initDone = false;
 private GUIStyle StatesLabel;
 
 void InitStyles()
 {
     initDone = true;
     StatesLabel = new GUIStyle(GUI.skin.label)
     {
         alignment = TextAnchor.MiddleLeft,
         margin = new RectOffset(),
         padding = new RectOffset(),
         fontSize = 15,
         fontStyle = FontStyle.Bold
     };
 }
 
 void OnInspectorGUI()
 {
     if (!initDone)
         InitStyles();
     // your GUI code
 }
Comment
Add comment · Show 6 · 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 vexe · Feb 01, 2014 at 02:25 AM 0
Share

THAN$$anonymous$$ YOU!!!!!!! - However, any idea why am I not getting a "Button"y look after using a GUI.skin.button to construct a style?

What I'm doing:

     UtilButton = new GUIStyle(GUI.skin.button)
     {
         alignment = TextAnchor.$$anonymous$$iddleCenter,
         fontSize = 20,
         padding = new RectOffset()
     };

             if (GUILayout.Button("▼", UtilButton, GUILayout.Width(20)))
                 states.$$anonymous$$oveElementDown(i);

What I'm getting (doesn't look like a button :/) :

alt text

fsm.png (16.5 kB)
avatar image vexe · Feb 01, 2014 at 02:48 AM 0
Share

Even if use a GUI.Button and pass the style there - it still doesn't look like a button - any ideas?

avatar image vexe · Feb 01, 2014 at 02:54 AM 0
Share

If I don't use the style however, I get a buttony like thing - eventhough I'm telling it to use the default style (GUI.skin.button)

avatar image vexe · Feb 01, 2014 at 02:55 AM 0
Share

Seems that I can't use the default GUI stuff in OnInspectorGUI?

avatar image vexe · Feb 01, 2014 at 02:58 AM 0
Share

Tried constructing with "EditorStyles.$$anonymous$$iButton" - still no button shape =(

Show more comments

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

20 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

Related Questions

GUI works fine in editor, but not in build 2 Answers

Optimizing OnGUI - Too many gui elements? 2 Answers

Setting up GUIStyles, best practice. 1 Answer

Access custom style from skin via string 1 Answer

OnGui Function help 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