Tribute to the original http://blog.csdn.net/chenggong2dm/article/details/25504611
Write in front:
This article creates a basic button. and write scripts that respond to click events.
Welcome everyone to correct, shoot bricks! Original very hard, if reproduced, please indicate the source.
Button--Buttons
In Ngui 3.5, button This control has been collated. such as changing the Creation method (search, drag and drop), such as merging the button in the 2.x and the image button. Personally think this is reasonable, wxpython button, do not divide the basic buttons, or the picture button, just created when the parameters are different (heavy).
1, creating widgets
If you have already created a widget (or if you have UI Root and camera), ignore this step. Skip directly to step 2. Otherwise, perform the following steps:
Make sure your project has imported NGUI (see my "NGUI 3.5 tutorial (i)", if it's unclear). Or create a new blank project, and then import Ngui.
In order to display the label, we need to create a new widget. This is the so-called gadget. On the menu, click "NGUI"-"Create"-"widgets".
After that, the UI Root is automatically created on the hierarchy panel, including (Camera, Container).
2, create Button
In Ngui 3.5, creating a button is special and can be accomplished by "searching" and then "dragging". The specific:
(1) In the project panel, search for "control", that is, search controls. After that, you'll find a bunch of Ngui controls.
(2) Locate the simple Button and drag it to the bottom of the hierarchy panel. I dragged it down here under the container.
In the inspector, you can see that the simple button has 4 components by default:
Transform, as the most basic component of unity, is no longer explained here.
Uiscript is a script that Ngui mounts on a button by default, primarily for operations such as specifying Atlas, depth, and so on.
UIButton is the script that Ngui mounts on the button by default, primarily specifying button state, such as normal, hover (also called hovering, passing, Hover), press (Pressed), disable (Disabled).
Box Collider is a box collider that hangs on the button by default. The effect is to have the button control have a collision property. The triggering of events in Ngui requires that the object have collision properties.
(3) Through the label's Text property, you can change the text to a simple button. Here I change to Button-1.
3, scripting , handling Button Click events
Create a C # script in the project panel by creating a new scripts folder, and then right---"create" and "C # script." Named as TestButton.cs:
[CSharp]View Plaincopy
- Using Unityengine;
- Using System.Collections;
- Public class Testbutton:monobehaviour {
- // Use this for initialization
- void Start () {
- }
- void OnClick () {
- Debug.Log ("ok! Get the click! ");
- }
- }
Save the script, then drag the script to the Control-simple button on the hierarchy panel, and then run:
OK, click the button will, will find printing information! button click event Success Response!
ps. button Display Gray (button disable) settings :
[CSharp]View Plaincopy
- This. Getcomponent<uibutton> (). IsEnabled = false;
When executed, the color of the disable state is automatically called (such as the default light gray) and the button is disabled.
NGUI 3.5 Tutorials (three) button buttons