New UI system messaging is designed with Monobehaviour to implement a custom interface so that callbacks from the message system can be accepted. When an object is specified by the execution of the message, all scripts that implement the custom interface for this object will be notified, and the specified method will be executed.
1. Define an interface icustommessagetarget inherit Ieventsystemhandler, so that when this type of message is sent, the object that implements the script for this interface will be notified.
using Unityengine; using Unityengine.eventsystems; using System.Collections; Public Interface icustommessagetarget:ieventsystemhandler{ void Message1 (); void Message2 ();}
2. Test script Custommessagetarget implement Icustommessagetarget, add to an object.
usingUnityengine;usingSystem.Collections; Public classCustommessagetarget:monobehaviour, icustommessagetarget{ Public voidMessage1 () {Debug.Log ("Message1 received."); } Public voidMessage2 () {Debug.Log ("Message2 received."); }}
This event is triggered when the following code is executed
null, (x, y) = = X.message1 ());
EventSystem also provides a number of events that can be triggered by simply implementing these interfaces in the script.
- Ipointerenterhandler-onpointerenter-called when a pointer enters the object
- ipointerexithandler-onpointerexit-called when a pointer exits the object
- Ipointerdownhandler-onpointerdown-called when a pointer was pressed on the object
- Ipointeruphandler-onpointerup-called when a pointer was released (called on the original The pressed object)
- Ipointerclickhandler-onpointerclick-called when a pointer are pressed and released on the same object
- Iinitializepotentialdraghandler-oninitializepotentialdrag-called when a drag target was found, can be used to Initialis E values
- Ibegindraghandler-onbegindrag-called on the Drag object when dragging are about to begin
- Idraghandler-ondrag-called on the Drag object if a drag is happening
- Ienddraghandler-onenddrag-called on the Drag object when a drag finishes
- Idrophandler-ondrop-called on the object where a drag finishes
- iscrollhandler-onscroll-called when a mouse wheel scrolls
- Iupdateselectedhandler-onupdateselected-called on the selected object each tick
- iselecthandler-onselect-called when the object becomes the selected object
- 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 when the submit button is pressed
- icancelhandler-oncancel-called when the Cancel button is pressed
Example:
usingUnityengine;usingSystem.Collections;usingUnityengine.eventsystems; Public classTestpointerenterevent:monobehaviour, Ipointerenterhandler, ipointerclickhandler{ Public voidonpointerenter (pointereventdata data) {Debug.Log ("Pointer Enter."); } Public voidOnpointerclick (pointereventdata data) {Debug.Log ("Pointer Click."); }}
Unity New UI Event System (EventSystem) Demo