Game Development Reference
In-Depth Information
Each style has options to define:
• A
Name
• A texture or text color for the different states of the control it's attached to
(
Normal
,
Hover
,
Active
, and
Focused
)
• The border, margin, padding, and overflow sizes for the control
(for each edge)
• A
Font
(with suitable size, style, alignment, word wrapping, and rich
text support options)
• A text clipping size
• Image position within the control
• Offset settings for the content within the control
• A fixed width and height
• Stretching options for the width and height
I recommended configuring a public
GUIStyle
property in a class and then
modifying it in
Editor
Inspector
when setting up a
GUIStyle
, for example:
using UnityEngine;
[ExecuteInEditMode]
public class GUIStyles : MonoBehaviour {
public GUIStyle;
void OnGUI() {
//Create a label using the GUIStyle property above
GUI.Label(new Rect(25, 15, 100, 30), "Label",
myGUIStyle);
}
}
You can also configure a
GUIStyle
in code, however it's not
recommended as the editor is just better at it.
It is worth noting that having too many different GUIStyles all over
the place can become very inefficient and hard to maintain. If you
find you are using a lot of
GUIStyles
then I'd recommend you create
a single script attached to a common object (say
Main Camera
) in
your scene with all your GUIStyle's defined and have each script take
GUIStyle
references from there.