Unity game development-Custom Event test demo, unitydemo
In the previous article, we wrote the unity game development custom message event. Click to open the link.
The following is a test demo.
First, punch in unity, create a new scenario, and then create an empty game object named EventObj.
Step 2: Test the code EventTest. as and drag it directly to the empty game object EentObj above.
The test code is as follows:
Using UnityEngine; using System. collections; public class EventTest: MonoBehaviour {// Use this for initializationvoid Start () {// message registration EventMgr. ins. addEventListener (ECoreEventType. EID_DEMO_TEST, OnEventTestDemoHandler);} // Update is called once per framevoid Update () {if (Input. getKeyDown (KeyCode. a) {// message distribution 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) ;}// message processing 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) {// after the message processing function is removed, Press A or B. The message will not print ("Press 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, B, the messages are received by the message processing function and the corresponding information is printed. When C is pressed, the message processing is removed. When A is pressed, the print statement in the message processing function is not executed.