Unity game development-Custom Event test demo, unitydemo

Source: Internet
Author: User

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.




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.