Custom Event Test Demo for Unity game development

Source: Internet
Author: User

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

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.