Http://blog.csdn.net/onerain88/article/details/18963539
. Uicamera function Introduction
It mainly includes the monitoring and distribution of UI events, covering all the gameobjects rendered for this camera.
Event sources include: Mouse, touch, keyboard, and handle.
Events include hovering, pressing/lifting, selecting/unselecting, clicking, double-clicking, dragging, releasing, text input, tips display, scroll wheel slide, and keyboard input.
2. uicamera implementation process
(1) attributes
Event Type: includes the UI and world, which are used to distinguish whether uicamera processes UI events from UI controls or 3D objects.
Eventmask: filters out objects that do not need to accept UI events.
Event sources: indicates the event source to be processed. For example, the iPhone may only need touch, and the PC platform may also need mouse. Select the keyboard check box.
Thresholds: it refers to the range of event errors. For example, mouse click refers to the offset error of two events on the UI, which cannot be greater than 10 pixels. When it is greater than 10 pixels, it is not a click event!
Axes and keys: Specifies the direction key/Joystick, which is bound to the keyboard.
(For the setting of a string such as horizontal, If you accidentally modify it, you cannot find the corresponding variable value based on the string. We recommend that you change it to the enumeration type)
(2) monitoring and distribution
In update (), process touch/click, text input, keyboard/Joystick input, and tip in sequence.
The most important thing is the processing of the touch/click event. The following uses the processing of the touch event processtouches () for analysis.
Ngui encapsulates a mouseortouch class to encapsulate the touch class in unity3d, including location, offset, collision to gameobject, and event flag.
In uicamera, the dictionary object mtouches with ID as key and mouseortouch as value is cached. In processtouches (), the input is provided by unity3d. gettouch () gets the touch object. If it is a new touch point, it is cached in mtouches to get the current touch point (mouseortouch object), and its attributes are calculated and set.
Each touch object must be processed by processtouch (bool pressed, bool unpressed). In this method, it is mainly used to differentiate and distribute different events.
Pressed and unpressed indicate the current status of the current touch object. Here, we use common click events as an example. based on our understanding of click events, we should know that the current status of the touch object is pressed = false, unpressed = true, and touchbegan = false, pressed! = NULL.
Let's first look at the processing of pressed = false. here we need to mainly offset the current mouseortouch "from the beginning to the present". When this offset is greater than the set mouse click/touch tap, the click event is canceled.
Then, when unpressed = true is processed, first determine whether the pressed object of the mouseortouch object is not null, and then determine whether the drag object of the current mouseortouch object is equal to the current object, the click event of the current mouseortouch object is not canceled in the previous step, and the overall offset of the mouseortouch object is smaller than the set drag value (the mouse drag/touch drag set in thresholds ), under these conditions, the "Click Event" is met, and The onclick message of the pressed object sent to the mouseortouch object is completed!
(The above analysis only applies to click events. To analyze the specific logic of other events, you can first determine the two parameters of the processtouch () method based on the event type, analyze the code in different branches according to the parameters)
3. differentiate multiple click events
Ngui provides a processing method for click events. If you want to add your own click events and distinguish them from ngui touch events, for example, assume that the map and the hud ui in our scenario can be clicked. Here we use a uibutton and 3D cube to simulate the scenario.
For a click event, if you click on the UI control, we will cancel the logic processing of the Custom Event. If you do not click on the UI control, we need to determine whether to click "Custom Region". If so, we will execute logic processing for custom events.
In this case, we can use uicamera's
Static public bool raycast (vector3 inpos, out raycasthit hit)
To determine whether the click event triggers the UI event. If yes, cancel the logic processing of the Custom Event.
(This method traverses and judges all uicamera in the scenario)
(PS: although this is equivalent to two UI collision detection, it avoids the sequence of judging custom events and calling Update () for UI events)
Ngui uicamera controls touch and mouse events