- Home /
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]
For style your code you should follow this **guide**. Writing code is little hard, but maintain its Code quality is really hard.
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;
}
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!
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$$", "");
}
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
![](https://koobas.hobune.stream/wayback/20220612112400im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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