Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by wkmccoy · Jun 20, 2011 at 01:28 AM · fontguiskinstyle

Unity Custom Styles and how to access in scripts

I use a custom skin for most of my projects, and usually have 15-20 custome styles defined in the skins script. I am still a bit confused on how to access the parameters of each of the custom Styles, such fontStyle. Sometimes I just want to bold a specific custome style I have, but just for one line. (say in a GUI.Label)

So I don't normally connect the script to the guiStyle such as. var customGuiStyle : GUIStyle;

I create them in the custome skin and then access them by putting ,"styleName" in my GUI.XXX lines... This seemed easier to port the Skin to a new project and not loose all the paramters I put in the inspector, like you do with a customStyle attached to a script.

I do hook the Custome Skin to my Main interface scrpit GUI.Skin = MySkin (where MySkin is the GUISkin linked by the Inspector)

since the custome Styles is an array, is there a way to access the parameters to say, enlarge the font or bold the font. So I can create fancier menus or text, without having to create another custom style?

I have accessed the MySkin.customStyles.Length ok, but was not sure on how to code access to a customStyle (say warLabels) would it use an index? [x]

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 MoliCat · Jul 10, 2014 at 07:28 PM 0
Share

For style your code you should follow this **guide**. Writing code is little hard, but maintain its Code quality is really hard.

1 Reply

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

Answer by jahroy · Jun 20, 2011 at 02:38 AM

Here's an example of how to manipulate a custom style named "warLabel" with code.

The example code changes the font size based on the screen resolution and does a couple other random things to demonstrate how you can manipulate anything you see in the inspector with code.

You can find everything you need to know about GUIStyles here.

Note: You can't change the font style or font size at runtime in iOS (you'd have to import multiple fonts and switch between them).

 var customSkin         :  GUISkin;

 var optimizedHeight    :  float  =  1200.00;
 
 var warLabelFontSize   :  float  =  14.0;
 var warLabelRect       :  Rectangle  =  Rect(100, 100, 100, 100);
 
 function OnGUI ()
 {
     GUI.skin  =  customSkin;

     /* lookup a GUIStyle by name */
 
     var labelStyle  :  GUIStyle  =  GUI.skin.GetStyle("warLabel");
 
     /* scale font size based on screen resolution */
 
     labelStyle.fontSize  =  getScale() * warLabelFontSize;
 
     /* make the font style bold */
 
     labelStyle.fontStyle  =  FontStyle.Bold;
 
     /* alter the x-value of the content offset */
 
     labelStyle.contentOffset.x  =  40.0;
 
     /* add 5 pixels of padding on the left */
 
     labelStyle.padding.left  =  5.0;
 
     /* print a label with the altered style */
 
     GUI.Label(warLabelRect, "I am scaled", labelStyle);
 
     /* the above is the same as this: */
 
     GUI.Label(warLabelRect, "I am scaled", "warLabel");
 }
 
 function getScale () : float
 {
     return Screen.height / optimizedHeight;
 }



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 wkmccoy · Jun 20, 2011 at 03:24 AM 0
Share

Ok, I was under the assumption I could just reference it since it was part of the skin. (rather than lookkng it up) Either way it gets me what I want, it was driving me crazy! Thanks. I was making variations of a style, just to increase the font size, so I was ending up with small, med , large of most of my sytles, and now I can simply create an adustment on the fly!

avatar image wkmccoy · Jun 20, 2011 at 03:31 AM 0
Share

Just found another option thought you might like to check out, which is interesting, since its basically the same idea (unless you know the index # your wanting to work, then its more direct)

theSkin.customStyles[1].name theSkin.customStyles[1]fontSize

// loop through the table (which problably what the GetStyles does) { string output = ""; GUISkin theSkin = (GUISkin) Selection.activeObject; for (int i=0; i { output += i + ": " + theSkin.customStyles[i].name + "\n"; }

     EditorUtility.DisplayDialog("Custom Style Names by Index", output, "O$$anonymous$$", "");
 }
avatar image jahroy · Jun 20, 2011 at 03:36 AM 0
Share

You could reference them by their index in the custom styles array, but that would be less clean in my opinion.

To do that you'd probably just be re-writing the GUISkin.GetStyle function by iterating over the customStyles array to look for the style.

Your style is part of the skin, but it's simply a member of the array named customStyles. Basically a GUISkin must be defined something like this somewhere:

 class GUISkin
 {
     var box           :  GUIStyle;
     var button        :  GUIStyle;
     var toggle        :  GUIStyle;
     var label         :  GUIStyle;
     var textField     :  GUIStyle;
 
     /* so on and so forth... */
 
     var customStyles  :  GUIStyle [];
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problems controlling the GUI skin / style 2 Answers

Font size and style overrides are only supported for dynamic fonts 2 Answers

Is there a way to measure the pixel with/height of a string with a given font? 2 Answers

How to find a font via script? 2 Answers

GUI Styles for iPhone/Android - font/button sizing? 3 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