Detailed description of the Unity5.0 EventSystem event System __unity3d

Source: Internet
Author: User

Reproduced from: http://www.manew.com/blog-56596-2917.html

UNITY5.0 has developed a new UI system Ugui, and the accompanying event system EventSystem can be used not only for the UI, but also for objects in the scene. There are a lot of instructions on the web, but just how to use it ... I've been through countless official manuals, including my own understanding, and the portal of official documents, and maybe you'll be interested.


I. Description of the EventSystem object


When we create any UI object in the scene, the hierarchy panel can see that the system automatically creates the object EventSystem, and you can see that there are three components under the object: EventSystem, Standaloneinputmodule, Touchinputmodule, the following two components are inherited from Baseinputmodule. The EventSystem component is primarily responsible for processing input, ray projections, and sending events. There can only be one EventSystem component in a scenario, and it requires the assistance of the Baseinputmodule type component to work. In the beginning, EventSystem will add the Baseinputmodule type component under the object that it belongs to an internal list The Updatemodule interface of these basic input modules is called through the interface Updatemodules interface in each update cycle, and then baseinputmodule modifies its state to ' Updated ' in the Updatemodule interface , then the Baseinputmodule process interface is invoked.

Baseinputmodule is a base class module that is responsible for sending input events (click, drag, select, etc.) to specific objects. All input modules under EventSystem must inherit from the Baseinputmodule component. The Standaloneinputmodule and Touchinputmodule components are standard input modules and touch input modules provided by the system, and we can implement our own input modules by inheriting Baseinputmodule.

In addition to the above two components, there is a very important component that we cannot see through the EventSystem object, which is the Baseraycaster component. Baseraycaster is also a base class, and the previous input module detects that the mouse event must have a ray casting component to determine the target object. The system realizes the Ray projection class component to have Physicsraycaster, Physics2draycaster, Graphicraycaster. This module is also able to inherit baseraycaster to achieve personalized customization.

In General, EventSystem is responsible for management, Baseinputmodule is responsible for input, Baseraycaster is responsible for determining the target object, the target object is responsible for receiving events and processing, and then a complete event system has.

In addition, in fact, these explanations are provided by the official, here is the English translation into Chinese, and the reorganization, with their own understanding, where there is a problem, please all the way fairy advice.

Official documentation is here: http://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html

Ii. the event system in Ugui

According to the instructions in the first section, EventSystem and Baseinputmodule are glued to an object, and these two modules are visible directly on the EventSystem object. So, Baseraycaster module ...

In fact, Ray detection, must be launched from the camera, then the Baseraycaster module must also be related to the camera is not simple.
For the UI module, we can see the Graphicraycaster component under the Canvas object. If the canvas rendering mode is sceenspace-overlay, then we cannot see the camera component. So it should be that graphicraycaster will do special processing for different rendering modes of the UI.
Because of the Graphicraycaster component, all UI objects on the canvas can accept the events emitted by the input module, and the handling of the specific events is described in section fourth.

Iii. using event systems in scene objects


Non-UI object in the scene, if you want to receive the input module's event, the same reason, you need to put a ray detection component to the camera. Physicsraycaster, Physics2draycaster These two components are used for the three-dimensional and 2D scene. Of course, it also requires the object of the scene to hang the collider ray before detection.

In fact, the official radiation detection is also done to explain, if the reading manual is not found, here is the Portal:

Http://docs.unity3d.com/Manual/Raycasters.html

If there is only one ray detection source in the scene: When a raycaster is present and enabled in the scene it'll be used by the EventSystem a query is Issued from a inputmodule.

If there are multiple ray detection sources in the scene: If multiple raycasters are used then they'll all have casting happen against them and the results would Be sorted based on distance to the elements.

Iv. Responding to events


1, the input module can detect events

Standaloneinputmodule and Touchinputmodule two components will detect some input operations, in the manner of events (message system) to notify the target object, then these two components support the main events are the following:

Ipointerenterhandler-onpointerenter-called when a pointer enters the object Ipointerexithandler-onpointerexit-call Ed when a pointer exits the object ipointerdownhandler-onpointerdown-called then a pointer is pressed on the object IP Ointeruphandler-onpointerup-called when a pointer was released (called on the original The pressed object) ipointerclic Khandler-onpointerclick-called when a pointer are pressed and released on the same object Iinitializepotentialdraghandl Er-oninitializepotentialdrag-called when a drag the target is found, can being used to initialise values Ibegindraghandler- Onbegindrag-called on the "drag object" when dragging are about to begin idraghandler-ondrag-called on the Drag object When a drag was happening ienddraghandler-onenddrag-called on the drag object when a drag finishes Idrophandler-ondro P-called on the object where a drag finishes iscrollhandler-onscroll-called when a mouse wheel scrolls iupdateselect Edhandler-onupdateseleCted-called on the selected object tick iselecthandler-onselect-called when the object becomes the selected obj  ECT ideselecthandler-ondeselect-called on the selected object becomes deselected imovehandler-onmove-called when a Move event occurs (left, right, up, down, ect) isubmithandler-onsubmit-called The submit button is pressed Icanc elhandler-oncancel-called when the Cancel button is pressed

As long as the target object's mono script implements the above interface, the input module notifies the target object of the detected events through these interfaces. Reference: http://docs.unity3d.com/Manual/SupportedEvents.html If you customize your input module, then these events must be useless.

2, the way to receive input events

1), the implementation of the interface of self-monitoring

Inherit the event interface provided by the input module in the Mono script, as shown in the following figure. The definition of interface can also check the official manual, http://docs.unity3d.com/ScriptReference/EventSystems.IBeginDragHandler.html this side has the definition of each interface, Be assured of daring places to go in. In addition, add the Objchooseevent Component object, must have collider oh.

public class Objchooseevent:monobehaviour,ipointerdownhandler,ipointeruphandler,iselecthandler, Ipointerclickhandler
{
    private eventsystem currevent;
    void Start ()
    {
        currevent = eventsystem.current;
    }
    void Update ()
    {

    } public
    void Onpointerdown (pointereventdata data)
    {
        print ("Onpointerdown..");
        Currevent.setselectedgameobject (Gameobject);
    }
    public void Onpointerup (Pointereventdata data)
    {
        print ("Onpointerup..");
    }

2), through the EventTrigger component monitoring events

This is an official component. On the object that needs to listen for the event, hang up the component, and then expand the configuration in the Inspector panel, and you will see that this component provides the monitoring of the event types supported by all input modules, as shown below.

The advantage of this approach is that when you select a type that you want to listen to, you can add multiple listener interfaces to the event type to manage it, and you know exactly where to respond to the event. If it's a way of inheriting interface, it will be scattered in n scripts, and if there is a problem, it will be very sour.

But this way through configuration, once the project more people collaboration, the complexity of the project, this drag and drop the configuration will have a lot of problems, such as a component removal, such as the response interface changed a name ~ ~ will lead to configuration loss, and the problem can not be found in time. Or the program's listener interface is different for certain conditions. So maybe you'll need a third way.

3), dynamically add EventTrigger components or modify components

program Dynamic Settings implementation. In our day-to-day development, we may need to dynamically change bound events, so how can we use C # code to control the binding trigger event? Here we'll introduce code control. ScriptControll.cs script.

<span style= "FONT-SIZE:18PX;"
>using Unityengine;
Using System.Collections.Generic;
Using Unityengine.events;

Using Unityengine.eventsystems;  public class Scriptcontrol:monobehaviour {//initialization void Start () {EventTrigger trigger
        = Transform.gameobject.getcomponent<eventtrigger> ();
        if (trigger = = NULL) Trigger = Transform.gameobject.addcomponent<eventtrigger> ();
        Instantiate delegates trigger.triggers = new list<eventtrigger.entry> ();
        Defines the event type to bind eventtrigger.entry Entry = new Eventtrigger.entry ();
        Set Event Type Entry.eventid = Eventtriggertype.pointerclick;
        Set callback function Entry.callback = new eventtrigger.triggerevent ();
        Unityaction<baseeventdata> callback = new unityaction<baseeventdata> (onscriptcontroll);
        Entry.callback.AddListener (callback);
	Add event trigger log to Gameobject Event Trigger component Trigger.triggers.Add (entry);
 } void Update () {   public void Onscriptcontroll (Baseeventdata arg0) {Debug.Log ("Test click"); }}</span>
In fact, http://www.cnblogs.com/zou90512/p/3995932.html this classmate's blog on these three methods have done a very detailed description.
Just EventTrigger external interface is not very friendly, cause we need to add a monitoring, as if around the N mountain Road curved, looking at the mood is not happy ... Instead, the blogger said the button's Click event to achieve a little meaning ... If the project is needed, maybe we can do the same.

Five, the EventSystem component provides some interesting interfaces


In fact, the documents have http://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html just maybe you didn't notice.
Variable:

  <span style= "FONT-SIZE:18PX;" >firstselectedgameobject: This value can be set in the panel, if you need a game to automatically select an object at startup, you need to click on the mouse.
  Currentselectedgameobject: The currently selected object, you can use this value to determine whether the current mouse clicks on the object, because you may have the
         ability to drag the camera, but you do not like to click on some objects when the function is responded to, So it's a good idea to judge by this variable. </span>

Interface:

  <span style= "FONT-SIZE:18PX;" >ispointerovergameobject: Whether the current mouse is on an object that the event system can detect.
  Setselectedgameobject: This interface may be overlooked, but it's great. Because when you click on the scene object, if you do not call this interface, your object is
       not received onselect event, the value of Currentselectedgameobject will not be set, you must call this interface in the Click event to set the selected object. </span>
<span style= "FONT-SIZE:18PX;" >ex: Public
void Onpointerclick (Pointereventdata eventdata)
{
    print ("Onpointerclick ...");
    Currevent.setselectedgameobject (Gameobject);
} </span>
Instead of looking for EventSystem objects in the scene, the EventSystem component has a current static variable, which is the object you want to use directly eventsystem.current.






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.