• 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
0
Question by cregox · Oct 27, 2010 at 06:41 PM · guiinspectorscriptingbasics

How to set up a default GUIStyle in the Inspector?

Why this doesn't work? This is C#.

using UnityEngine; using System.Collections;

public class Whatever : MonoBehaviour { public string nomeAvatar = ""; public GUIStyle estiloNome = "label";

 void OnGUI () {
     nomeAvatar = GUI.TextField(new Rect(10,10,200,20), nomeAvatar, 25, estiloNome);
 }

}

I wanted to have a custom GUIStyle in the inspector but default to "label".

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

2 Replies

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

Answer by jahroy · Sep 21, 2011 at 05:43 PM

In UnityScript:

 var estiloNome : GUIStyle = GUISkin.GetStyle("label");
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 cregox · Sep 21, 2011 at 06:26 PM 0
Share

hmm, if the equivalent public GUIStyle estiloNome = new GUISkin.GetStyle("label"); works on C#, I guess we have a winner! Can you test & post this?

avatar image jahroy · Sep 21, 2011 at 07:18 PM 0
Share

The following worked for me:

 void OnGUI () 
 {
     if ( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X) ) {
         print(GUI.skin.GetStyle("toggle").padding.left);
     }
 }
avatar image
1

Answer by Kalu · Jul 11, 2011 at 09:31 AM

Just declare ur GUISkin like this:

public GUISkin customSkin; (and don't forget to put ur GuiSkin in this public var in Inspector window)

And in OnGui, just whrite

GUI.skin = customSkin;

Now, u just have to edit the different setting of ur GuiSkin in Inspector window...

ps: old answer but that can be helpfull for someonelse

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 cregox · Sep 21, 2011 at 04:43 PM 0
Share

This can be useful indeed, but it's not what I asked. I wanted to set the default style in code and not in the inspector. I can't remember why, but there's usually a good reason to do something code-only without relying on the visual editor.

avatar image syclamoth · Sep 21, 2011 at 04:57 PM 0
Share

Really? I can't think of a reason in this case- since you can just change the skin in code anyway, and a GUI to that is simpler to use.

avatar image cregox · Sep 21, 2011 at 05:09 PM 0
Share

@syclamoth really. In this case picture if I'm creating the custom skin in run time, by the user, for instance. Or I simply want to leave the customization on code for Version Controlling it. It's not always the case, but yeah, really, there is usually a good reason for coding up.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to add UnityEvent using Custom Inspector? 1 Answer

Adding Text to Canvas via script 4 Answers

Drawing a property in the inspector whose type could be one of several custom classes 2 Answers

Crash on Inspector during play 0 Answers

Custom Inspector Invocation behaviour? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges