A Label component renders a piece of text on screen, in game space. By default it is sorted and drawn with all sprite and tile graphics. The component has a set of properties that governs how the text is rendered. Defold’s GUI supports text but it can be tricky to place GUI elements in the game world. Labels make this easier.
To create a Label component, right click the game object and selecting Add Component ▸ Label.
(If you want to instantiate several labels from the same template you can alternatively make a new label component file: right click a folder in the Assets browser and select New... ▸ Label, then add the file as component to any game objects)
Set the Font property to the font you want to use and make sure to set the Material property to a material that matches the font type:
Apart from the properties Id, Position, Rotation and Scale the following component specific properties exist:
Note that the default material has shadow rendering disabled for performance reasons.
The Blend Mode property defines how the component graphics should be blended with the graphics behind it. These are the available blend modes and how they are calculated:
src.a * src.rgb + (1 - src.a) * dst.rgb
src.rgb + dst.rgb
src.rgb * dst.rgb
By setting the Pivot property you can change the alignment mode for the text.
South, the text is center-aligned.
Westmodes, the text is left-aligned.
Eastmodes, the text is right-aligned.
You can manipulate labels in runtime by getting and setting the label text as well as the various other properties.
numberfor uniform scaling or a
vector3for individual scaling along each axis.
function init(self) -- Set the text of the "my_label" component in the same game object -- as this script. label.set_text("#my_label", "New text") end
function init(self) -- Set the color of the "my_label" component in the same game object -- as this script. Color is a RGBA value stored in a vector4. local grey = vmath.vector4(0.5, 0.5, 0.5, 1.0) go.set("#my_label", "color", grey) -- ...and remove the outline, by setting its alpha to 0... go.set("#my_label", "outline.w", 0) -- ...and scale it x2 along x axis. local scale_x = go.get("#my_label", "scale.x") go.set("#my_label", "scale.x", scale_x * 2) end
The game.project file has a few project settings related to labels.