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?

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;