Unity3D game development 15th NGUI 2

Source: Internet
Author: User

Next, we will explain the UILabel, which is a script used to display text and inherited from the UIWidget. We create a label with attributes such:


All labels must be Font before they can work properly. This Font can make Dynamic (reference Unity Font) or Bitmap Font-the Font embedded in Atlas. Dynamic fonts are more stable because they do not require you to create symbols in advance, but Bitmap fonts can be rendered on the same draw call as other things of your atlas, and these fonts can be beautified through photoshop.

Modify labels to change the label alignment. Top-left, Left, and Bottom-left are left-aligned. Top, Center, or Bottom are intermediate alignment. Top-right, Right, and Bottom-right are right-aligned.

When using the Dynamic Font, you can set the Font Size and style of the label. You can also set a material for rendering.

The next big box, which you may have guessed, is used to input text. Multi-line text input by default, unless restricted by the following Max Lines attribute.

The Overflow operation allows you to determine the processing method after the lable text exceeds the allowed space.


  • The Shrink Content is automatically scaled. Control the Dynamic font together with the Keep Crisp settings to reduce the font size rather than simply scaling. This ensures clarity regardless of whether the label is scaled or not.
  • Clamp Content removes unnecessary text.
  • Resize Freely automatically adjusts dimension based on the input text content. Manual adjustment is not required.
  • Resize Height keeps the width unchanged and increases the Height if necessary.
The Spacing attribute controls the Character Spacing. Both positive and negative values are acceptable. In pixels.

The Max Lines mentioned above are used to control the maximum number of rows. 0 indicates no restriction.

If Encoding is disabled, no color or emoticons are required. Enabled by default.

You can give the label Gradient through the bottom and top color of Gradient. For example:


The text can contain shadow or outlineEffect. Shadow uses twice the geometry and outline uses five times-so be careful with this attribute. The Distance parameter controls the Distance between shadow or outline and the original text, in pixels.


Next we will explain UISprite, which is the most practical in NGUI. Use a part of the atlas texture to draw sprite and inherit all functions of the UIWidget. attributes include:


  • Sprite needs an Atlas first. Click the Atals button to list the existing atlas instances in the current project. To create a new atlas, use the Atlas Maker tool (NGUI-> Open-> Atlas Maker ).
  • You can click the Edit button next to the Sprite selection button to Edit the selected sprite. Here we allow sprite to be added to adjust the boundary of 9-sliced.
  • By default, the newly added sprite will be of the Simple type. This means that they are similar to UITextures, except that they are from atlas.
  • Sprite Type is Filled, so that a cool-down progress bar can be created.
  • Sprite Type is Tiled, so that your sprite will repeat filling an area. Try not to use a sprite that is too small, because it creates many triangles. It is also a good idea to give your sprite A 1-pixel border, which will be excluded (the edges of all tiled images look the same ).
  • If Sprite Type is Sliced, you can create a 9-slicing sprite. It takes effect only when Border is specified. (Click the Edit button mentioned above ). An Application of sliced sprite is the background of the button.
Use the "Make Pixel-Perfect" option (the new version is Snap) to allow UISprite to use its original size. If your original texture is 300*200 pixels, the width and height corresponding to the dimensions of UISprite will be adjusted to this value. Note that this has no effect on Tiled and Sliced sprite-they have their own rules.

Next, let's take a look at UIButton. A Button is a very simple component that provides the following functions: to receive hover, pressed, and click events, and to modify the specified sprite color. The Image Button component and the Button can be added to a game object at the same time. The Image Button is used to switch sprite. We create a new Button, which mainly contains a Sprite and a label of the background, such:


Attributes include:


In the sprite of Background, we specify a texture image, modify the Color of the Widget, and then modify the text content and Color of the Label, such:


Let's take a look at the button listening event. The first method is: Create a script and the code is as follows:

void OnClick(){Debug.Log("------test");}
Then bind it directly to the button. When we click it, the log content will be printed.

The second method is to use SendMessage. After selecting the Button, select Component> Interaction> Button Message from the Unity navigation menu bar.

Target: The game object that receives button messages.

Function Name: method used to receive button messages. scripts with this method must be bound to the Target object above.

Trigger: the Trigger event. OnClick is obviously a click.

Include Children: whether to send this click event to all sub-objects of the object.

Set an empty object, write all messages in a script, assign values to the empty object, add the UIButton Message component to the control, and pull the empty object into the Target of the component, select the corresponding Function Name (such as The OnClick Function. For multiple buttons, you only need to add the UIButton Message component and select the corresponding function. For example:


The third method is to use UIListener, which is also recommended. Select Component> NGUI> Internal> Event Listener in the Unity navigation bar. It can be attached to a button without any parameters, such:


Modify the Test script code as follows:

Void Awake () {Debug. log ("---- 111"); // obtain the GameObject button = GameObject. find ("UI Root (2D)/Camera/Anchor/Panel/Button"); // set the listener for this Button, pointing to the UIEventListener in the ButtonClick method of this class. get (button ). onClick = buttonClick;} // Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {} void buttonClick (GameObject button) {Debug. log ("GameObject:" + button. name );}

Then bind it directly to the button. When we click it, the log content will be printed.

Next, let's take a look at UIInput, an input box, just like textfield. Its structure is also like button, a backgroud and a label, for example:
Let's take a look at its attributes, such:
Label: The UILabel in which the text is displayed.

Starting Value: the word that is displayed in the input box.

Saved As: Save the input content to prefabs.

Active TextColor: The color displayed after the text box is activated

Inactive Color: default status Color

Caret Color: The Color of the inserted characters.

Selection Color: select the Color of Characters in the status

Input Type: the Input Type (standard) standard, (AutoCorrect) automatic correction, (Password) Password

Validation: input type Restriction

Character Limit: Maximum number of input characters

On Submit: Event distribution after input

Create an input, for example:




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.