[Unity3d] get twice the result with half the effort: Use ngui tutorials and Instances

Source: Internet
Author: User

Original address: http://www.tasharen.com /? Page_id = 185

Ngui: click send


Ngui Tutorial: Step 1-scene

1. Create a new scenario (new scene ).
2. Select and delete maincamera in the scenario.
3. Select create a new UI under the ngui menu to open the UI creation wizard.


4. You can change the basic UI parameters in the create wizard. Select the default layer and click Create Your UI.
5. In this way, your UI is created.


Note: If you want to skip step 2 in an existing ngui project and select a different UI Layer, make sure that the non-Gui camera does not render the UI Layer.

Before creating a specific UI control, let's take a look at what the UI wizard has done for us.

1. There is a uiroot script on the UI root (2D) object. This script will re-adjust the game object to match your screen height with automatic and manual selection of height. It allows you to set the coordinates of widgets in pixels, and is relatively smaller than the remaining objects in the game world.

2. The camera object contains the camera and uicamera scripts. The uicamera script contains the ngui Event System ).

3. Anchor contains the uianchor script. Although this script can be appended to the control, the upper half pixel offset of the Windows machine can be avoided here.

4. The Panel object has a uipanel script. uipanel is a container that contains all the UI widgets and is responsible for optimizing the components in combination to reduce the call of the drawing command.

At the same time, you may also note that the Panel (dashboard) gameobject is now selected. That is to say, all the parts added below will be used as their sub-objects.


The corresponding scenarios in the tutorial 1-scene plug-in include:



Ngui Tutorial: Step 2-Spirit

Now let's add some controls and select ngui menu> Create a widget from the ngui menu.

This wizard will guide you through creating some basic controls. Select the image set atlas and font used.

Because it is a newly created project, it is ineffective to press the Atlas and font buttons. On the contrary, most recently used elements are automatically displayed.

What you want to do is drag the Atlas and font you want to use to enter the area next to the button. We will first select Atlas as scifi Atlas, and the font is scifi font-normal.


The following is the template issue. You can choose from a bunch of templates to select the parts you want to create.

Note that I am talking about "templates ". This template helps you create it easily. When you actually use ngui, you often spend a lot of time copying and pasting the entire widget, not just using this wizard. Now, let's create the first control.

1. Suppose we chose scifi atlas for Atlas, and we chose dark for Sprite.
2. Click the add to button (if the button is gray, it is because you did not select Atlas ).. If you do not know how to find it: in the project window, enter scifi.
3. Your sprite has been created and selected. But it is very small. When you use ngui, you always need to move parts, compress them, and rotate them through transform.


One very important thing is that the control in the same atlas does not use the Z axis. However, you can adjust the depth parameter to make adjustments before and after, if you use multiple atlase (or multiple UI panels) at the end of the subsequent project, you can adjust the Z axis transformation. The reason is simple: This allows you to create completely smooth windows, you can turn to one side, and the order of the parts will not be messed up during painting.

Before learning from the previous chapter, you can try to add more controls, such as adding a sliced sprite (a Sprite containing nine slices, the best choice for creating a control with a fixed border ), one tiled sprite (a Sprite scales to fill the entire custom area) and filled sprite (each sprite has a separate parameter to control which are visible, it is often used for progress bars or scroll bars ). It does not matter if you cannot find them. The following tutorial will explain them.


The corresponding scenarios in the plug-in tutorial-spirit include:




Ngui Tutorial: Step 3-sliced Sprite

1. Select the Panel object and use the control Wizard to add a "dark" sliced sprite from Atlas. What is 9-slicing? View here
2. Adjust the scale to (500,500, 1 ).
3. Add another sliced Sprite and set the scale to (, 40, 1 ).
4. Change the position of the second sprite to (0,230, 0 ).
5. Change the sprite's dark to light.
6. Adjust the colors of the two sprite until you like their positions.
7. Adjust the depth so that the colored Sprite is in front of the sliced Sprite.
Now it looks like a small window.


The corresponding scenarios in the plug-in tutorial 3-sliced sprite include:





Ngui Tutorial: Step 4-tiled Sprite

1. Select the Panel object, add tiled Sprite, and select honeycomb Sprite.
2. Set its position to (0,-494,457) and scale to (, 1 ).
3. You need to set the order before and after the deep adjustment to bring it back to the front of the background. At the same time, do not forget to adjust the depth of the window background (tune back ).

Play with this spirit. Try what happens when you zoom it out again, or adjust its color to make it look better.

You can check the generated geometric mesh by selecting the geometry of debug info in the panel object at any time and clicking your component in the scene.

After adding so many things, only one painting instruction is still called.


The corresponding scenarios in the plug-in tutorial 4-tiled sprite include:





Ngui Tutorial: Step 5-label

1. Select a panel object and add a label control in the widget navigation template as described earlier.
2. after clicking the add to button, your tag should have been added to the scenario and selected. If the add to button is unavailable, it is because you have not specified the font, you can specify "scifi font-normal" in the font bar from the project panel ".
3. In the inpector panel, add some text to the text item of the newly added uilabel. For example, hello World!
4. Move the tag to (0,234, 0) to display it on the title bar.

You can also create a different label and use different fonts. Take some time to study the color embedding function. You can use hexadecimal values to describe different colors (paste them directly from PS ).

[Ff0000] indicates the red color. insert [-] to restore the original color, and the text between them will change to red ..
Because the same font and material are used, the entire scenario is still a drawcall.


The corresponding scenarios in the tutorial 5-label plug-in include:





Ngui Tutorial: Step 6-button

Now, you can easily create something that looks like a button, but you still don't know how to make it accept click events.

However, this is very simple. Any collision event will receive the event it should have.

Therefore, for the simplest button, you only need to select attach a collider under ngui to give the control: ngui menu-> attach a collider.

Fortunately, the button template has been embedded in ngui. Let's take a look.
As before, select panel and use the Wizard to create a button.

You can see that the button is no longer a simple gameobject, but there are actually multiple parts.

Because buttons are actually composed of several parts: one is the background, the other is the tag, and some scripts are used to process button events.

This is the ngui feature. Multiple widgets are combined into powerful controls like building blocks.

1. uibuttoncolor: The color when the mouse is hovering over the button or pressed.
2. uibuttonscale: click the button to zoom in when you hover your mouse over the button.
3. uibuttonoffset: the button is shifted to the lower right when the button is pressed.
4. uibuttonsound: Play the sound when the button is pressed.

Of course there are other components, such as uibuttonplayanimation and uibuttonmessage, You need to explore them yourself.

You can also add a Custom Event (monobehaviour) to process more events. Detailed Directory

  • Void onhover (bool isover)-Sent out when the mouse hovers over the collider or moves away from it.Not sent on touch-based devices.
  • Void onpress (bool isdown)-Sent when a mouse button (or touch event) gets pressed over the Collider (with 'true') and when it gets released (with 'false', sent to the same Collider
    Even if it's released elsewhere ).
  • Void onclick ()-Sent to a mouse button or touch event gets released on the same collider as onpress.Uicamera. currenttouchidTells you which button was clicked.
  • Void ondoubleclick ()-Sent when the click happens twice within a fourth of a second.Uicamera. currenttouchidTells you which button was clicked.
  • Void onselect (bool selected)-Same as onclick, but once a collider is selected it will not receive any further onselect events until you select some Other collider.
  • Void ondrag (vector2 delta)-Sent when the mouse or touch is moving in between of onpress (true) and onpress (false ).
  • Void ondrop (gameobject drag)-Sent out to the collider under the mouse or touch when onpress (false) is called over a different collider than triggered the onpress (true) event.
    The passed parameter is the game object of the collider that has ed the onpress (true) event.
  • Void oninput (string text)-Sent to the same collider that specified ed onselect (true) Message After typing something. You likely won't need this, but it's used by uiinput
  • Void ontooltip (bool show)-Sent after the mouse hovers over a collider without moving for longerTooltipdelay, And when the tooltip shocould be hidden. Not sent
    On touch-based devices.
  • Void onscroll (float delta)Is sent out when the mouse scroll wheel is moved.
  • Void onkey (keycode key)Is sent when keyboard or controller input is used.


The corresponding scenarios in the tutorial 6-button plug-in include:




Ngui Tutorial: Step 7-Slider


Next let's add a slider.

1. Use the widget tool and select the slider template.
2. Use button Sprite for foreground and dark Sprite for background. Thumb can also use a button.
3. Select panel and click Add.

In this way, a slider scroll bar is ready.

You can select a slider to adjust the color and size of the game object. However, we recommend that you do not adjust the sub-objects under the control object.

In this way, you can keep them in size when you rotate them, and the internal parts can continue to work under coordinates. Of course, you can also try another vertical slider.


The corresponding scenarios in the plug-in tutorial 7-slider include:




Ngui Tutorial: Step 8: checkbox

Next let's check the box:

1. Click "X" for the mask and "dark" for the background ".
2. Create a check box and select the Panel object to create a new gameobject.
3. Add more check boxes to this gameobject.
4. select option items on these check boxes.
5. Run the program and click the check box. Check the effect.


If each step is correct, you cannot select two items at the same time: because after the "radio button root" parameter is selected, your check box will become a single choice button, they put the same root directory in A group: The transform object you specified.
You can also attach some other event processing functions, such as uicheckboxcontrolledcomponent and uicheckboxcontrolledobject.

It can be used to enable or disable a specified component or game object in the check box status.
You can try to make three check boxes to control the display and hiding of the red, green, and blue controls.


The corresponding scenarios in the tutorial 8-checkbox plug-in include:




Ngui Tutorial: Step 9-Input

Create an input box. Any text tag can be converted into an input box by adding the collision box uiinput script. Of course, it will be better if there is a background.

Fortunately, the input template has done all this for us:
1. Use the widget tool to create an input box.
2. You can use "dark" Sprite or "highlight" Sprite as the background.
3. Keep adjusting until debugging is correct.

If you click play to run the game, you can directly enter text in the input box. on iOS and Android devices, the screen keyboard is automatically displayed. This input box also supports the new input method (IME = input method) in unity3.4.1, so that you can enter Chinese (Translator's note: During the test, you can directly enter Chinese without displaying the text. Click to download the Chinese font, and drag it to the font column of the label ).

Currently, the only platform that does not support flash, because unity Flash does not support IME input.


The corresponding scenarios in the tutorial 9-input plug-in include:


Note: For ngui Chinese Input, You need to download a separate Chinese font (or make one by yourself) and drag it to the corresponding inpextor panel:


In this way, you can enter Chinese characters:



Ngui Tutorial: Step 10-3d

We have been working on the 2D interface above, but we can easily turn it into a 3D interface. Let's continue!

1. Remove the panel from anchor, that is, it is no longer a sub-object of anchor, and the UI root acts as its parent object.
2. Delete anchor (this tutorial only uses static UI ).
3. Change the camera to Perspective Projection perspective projection.
4. Change the camera's near to 0.1 and far to 4.0.
5. Move the camera a little later, probably at (550.
6. Give the Panel an angle. Here we set the rotation to (0,345, 0 ).
7. Add panwithmouse to camera to make it look more dazzling.

Click RUN game and move the mouse around. If everything is correct, you will see a very good window.

Note that these seven steps are only required after the 2D UI is created at the beginning.

If you create a 3D UI in step 1, these are no longer needed.

Finally, what kind of UI you need depends on your imagination. :)


The corresponding scenarios in the tutorial 10-3D plug-in include:


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.