Above wrote Unity game development Custom Message Event Click Open Link
Here is the test demo
First, punch in unity, create a new scene, and then create a new empty game object, in the eventobj
The second step, the test code eventtest.as, directly dragged to the above empty game object Eentobj
The test code is as follows:
Using unityengine;using System.collections;public class Eventtest:monobehaviour {//Use this for initializationvoid Star T () {//Message registration EventMgr.Ins.AddEventListener (ecoreeventtype.eid_demo_test, Oneventtestdemohandler);} Update is called once per framevoid Update () {if (Input.getkeydown (KEYCODE.A)) {//Message dispatch print ("dispatch.."); EventMgr.Ins.DispatchCoreEvent (New Coreevent (ecoreeventtype.eid_demo_test,0));} if (Input.getkeydown (keycode.b)) {EventMgr.Ins.DispatchCoreEvent (new coreevent (ecoreeventtype.eid_demo_test,1));} if (Input.getkeydown (keycode.c)) {EventMgr.Ins.DispatchCoreEvent (new coreevent (ecoreeventtype.eid_demo_test,2));}} The message handler function void Oneventtestdemohandler (Coreevent evt) {if (evt. EventCode = = 0) {print ("you press A");} else if (evt. EventCode = = 1) {print ("you press B");} else if (evt. EventCode = = 2) {//Remove the message processing function, press the A or B key, the message will not be printed print ("Presses C and remove this EventHandler"); EventMgr.Ins.RemoveEventListener (Ecoreeventtype.eid_demo_test,oneventtestdemohandler);}}}
Run the test log as follows
As you can see, when you press A, and B, the distributed message is received by the message handler and the corresponding information is printed. When C is pressed, the message processing is removed and the print statement in the message handler is not executed when a is pressed
Custom Event Test Demo for Unity game development