Game Development Reference
In-Depth Information
The
Label
control also supports using a Texture for its contents, but not both text
and a texture at the same time. However, you can layer Labels and other controls
on top of each other to achieve the same effect (controls are drawn implicitly in the
order they are called), for example:
public Texture2D myTexture;
void Start() {
myTexture = new Texture2D(125, 15);
}
void OnGUI() {
//Draw a texture
GUI.Label(new Rect(125, 15, 100, 30), myTexture);
//Draw some text on top of the texture using a label
GUI.Label(new Rect(125, 15, 100, 30), "Text overlay");
}
You can override the order in which controls are drawn by setting
GUI.depth = /*<depth number>*/
; in between calls; however,
I would advise against this unless you have a desperate need.
The texture will then be drawn to fit the dimensions of the
Label
field, By default
it scales on the shortest dimension appropriately. This too can be altered using
GUIStyle
to alter the fixed width and height or even its stretch characteristics.
GUIStyles
and
GUISkins
are explained in the later
GUI styles
and skins
section.
Texture drawing
Not specifically a control in itself, the GUI framework also gives you the ability
to simply draw a Texture to the screen Granted there is little difference to using
DrawTexture
function instead of a Label with a texture or any other control. (Just
another facet of the evolution of the legacy GUI). This is, in effect, the same as the
previous
Label
control but instead of text it only draws a texture, for example:
public Texture2D myTexture;
void Start() {