Chapter 2. Creating Widgets

In this chapter, we will create our first sprite widget and understand how it works. Then we will create one sample of each important widget template, and analyze their corresponding parameters so that you know how to create and configure them.

At the end of this chapter, we will have a functional main menu with most of NGUI's widgets.

Creating our first widget

We will create our first sprite widget to display our main menu's background window. In order to do that easily, NGUI has a Widget Wizard with a few templates for us.

Widget Wizard

The Widget Wizard can be opened by navigating to NGUI | Open | Widget Wizard. It will look like the following screenshot:

Widget Wizard

As you can see in the previous screenshot, to create a widget, you require Atlas and Font to be configured. As said in Chapter 1, Getting Started with NGUI, an atlas is a large texture containing the sprites that you need to create your UI. For the rest of this chapter, we will use the default atlas named SciFi Atlas , which is included in the plugin.

Selecting an atlas

Let's select our default SciFi atlas, which contains the necessary sprites, as follows:

  1. In the Project view, navigate to Assets | NGUI | Examples | Atlases | SciFi.
  2. Drag-and-drop the prefab SciFi Atlas.prefab in the Atlas field.
  3. Drag-and-drop the prefab SciFi Font – Header.prefab in the Font field.

We have our Atlas and Font prefabs selected. We can now create a widget from a template.

Creating a widget from a template

Let's create a widget from a template by performing the following steps:

  1. Click on the drop-down menu next to the Template field.
  2. Select the Sprite option as Template.
  3. Click on the drop-down menu next to the Sprite field.
  4. Select the sprite named Dark.
  5. Leave the Pivot option as Center.
  6. Make sure you have selected Panel in the Hierarchy view.
  7. Click on the Add To button.

Ok, our sprite widget has been created!

Note

The widget wizard adds the new widget as the child of the selected GameObject or panel. If you have selected the wrong GameObject, you can still drag-and-drop the new widget into the right GameObject after it is created.

Transforming widgets

We have created our first widget: Sprite (Dark). Select it in the Hierarchy view and try the following manipulations to change its transform values.

Moving widgets

In the Scene view, you can use the handles to move your widget or you may enter coordinates directly in the Inspector view's X, Y, or Z parameters. The following screenshot is of the Scene view with the three parameters visible:

Moving widgets

You should always leave the Z coordinates at 0. If you need to place a widget behind or in front of another, use the Back and Forward buttons in the Inspector view to control your widget's depth.

Tip

You may move your widget on only one axis by pressing Shift before you click on the axis's handle.

Rotating widgets

On the Scene view, place your mouse cursor on the outside of any blue circle surrounding your widget. Your cursor will have a rotation icon next to it. You can now keep your left mouse button pressed and move your mouse to rotate the widget.

Tip

By default, rotation is set to have a 15 degrees step. If you wish to have a more precise rotation—a 1 degree step—just hold Shift while rotating.

Scaling widgets

You may have noticed that in the Inspector view, the scale value is grayed out. That's because you should use the Dimensions parameter of the UISprite component instead.

On the Scene view, place your mouse cursor on any blue circle surrounding your widget. Your cursor will have a resize icon next to it. You can now click and drag your mouse to resize the widget.

Tip

Using the blue handles will not keep your widget centered to its current position. If you want to resize your widget proportionally on both sides, click on the space in front of the X or Y parameter of Dimensions of the UISprite component and drag your mouse left or right.

To keep everything pixel perfect, you should avoid scaling widgets up or down with Unity's scale tool. Try to do everything with Dimensions. Let's see what other parameters we have for widgets.

Common widget parameters

Select Sprite (Dark) and you will find the parameters seen in the following screenshot in the Inspector view:

Common widget parameters

These parameters exist for any type of widget. Let's see what they are:

  • Color Tint: This is the widget's alpha-enabled color.
  • Clipboard: If you click on the Copy button, the current Color Tint selection is copied to this parameter. If you click on the Paste button, the Clipboard parameter's selection will be pasted to Color Tint.
  • Pivot: This presents two sets of buttons that can be used to choose which corner or side you want the widget's pivot to be placed.
  • Depth: This can be used to display your widget in front or behind others.
  • Dimensions: This can be used to display size in pixels instead of scaling.

Now that we have seen the widget parameters, please enter these Dimensions for our newly created Sprite (Dark): 1300 x 850.

Our sprite window just got enormous and ugly. Why? Because it's a 15 x 15 sprite stretched to 1300 x 850! Let's talk about sprites and see how we can correct this.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.212.217