Game Development Reference
In-Depth Information
Automatic layouts and options
You can obviously place
UI
components within the
Canvas
and place them
(if you wish), but there are those situations where you need to be able to either
group or order elements together, such as:
• A list box
• A grid
• A scrollable area
In the legacy GUI system, this was done with separate controls that had layout
options built-in. For the new
Unity UI
however, this has been broken up into a more
component-based system with new grouping components, these can be attached to
any GameObject to organize the elements attached to that GameObject as children.
The types of groups you can create are:
•
Horizontal Layout Groups
•
Vertical Layout Groups
•
Grid Layout Groups
Groups don't have to be static top level elements; like with anything
else in the new system, everything is flexible. Want a
Horizontal Layout
Group
within a
Vertical Layout Group
within a
Grid Layout Group
,
then knock yourself out, as there are no limitations (well other than
common sense, it still has to look good doesn't it).
Horizontal Layout Group
With the horizontal layout, it will automatically place child GameObjects side by
side (columns) within the group's
Rect Transform
area. For example, we can create
a simple horizontal list (like what could be used to list a selection of in-app purchase
items) by creating the following:
1.
Create a
Canvas
in the scene (
Create
|
UI
|
Canvas
).
2.
Right-click on the new
Canvas
and add an empty GameObject (
Create Empty
).
3.
Rename this new
GameObject
to
Horizontal Layout Group
.
4.
Select the
HorizontalLayoutGroup
GameObject object and add a
Horizontal
Layout Group
component by clicking on
GameObject
by navigating to
Add
Component
|
Layout
|
Horizontal Layout Group
in the
Inspector
(or just searching for it when clicking on the
Add Component
button).