Book Image

NGUI for Unity

By : Charles Bernardoff (EURO)
Book Image

NGUI for Unity

By: Charles Bernardoff (EURO)

Overview of this book

Table of Contents (14 chapters)

Events methods


When using C# with NGUI, there are some methods that you will regularly use when you need to know if your object is currently hovered upon, pressed, or clicked.

If you attach a script to any object with a collider on it (for example, a button or a 3D object), you can add the following useful methods in the script to catch events:

  • OnHover(bool state): This method is called when the object is hovered or unhovered. The state bool gives the hover state; if state is true, the cursor just entered the object's collider. If state is false, the cursor has just left the collider's bounds.

  • OnPress(bool state): This method works in the exact same way as the previous OnHover() method, except it is called when the object is pressed. It works with a touch or click. If you need to know which mouse button was used to press the object, use the UICamera.currentTouchID variable; if this int is equal to -1, it's a left-click. If it's equal to -2, it's a right-click. Finally, if it's equal to -3...