Custom Interactive events for the server and client or page flow of unity game development

Source: Internet
Author: User

Introduction:

1. Interaction of the game page with the page

(1) Equip backpack


(2) Equipment Information page





In game development, between 2D pages, sometimes with relevance, such as the game's backpack system, players into the backpack system (see equipment backpack and equipment Information page), click on equipment, view equipment Information page, General card games These information pages are accompanied by additional equipment

Property value functions, such as "Let Go of the Three Kingdoms" in the equipment of the enhanced function, the player spent a certain amount of game money enhanced equipment, the equipment information page equipment properties will be refreshed. However, after closing the equipment information page, the backpack system

The corresponding description of the equipment in the page also has to change, but how does the backpack system know that the equipment information page has changed the original property values, then this is used to register the event monitoring and distribution. In other words, if the equipment is complete,

Hardening, when the equipment information page is closed, to send a message to the Backpack system page, and the backpack system in the open when the registration of a message processing function to process the message sent, when the message sent to the information page

, refresh the Backpack system page in the message handling function. This completes the consistency of the data


2. Client-to-server interaction

In the above mentioned, equipment hardening function, this involves the client and server interaction, when the player clicks the hardening button, it is necessary to send a hardening request to the server, before sending, the client needs to register a message processing function, waiting for the server hardening success, The receiving server sends a message to the client for hardening success or not, and after the client receives the message, it enhances the successful effect broadcast and refreshes the page.


The above two cases involve the interaction of the game elements, in which the page and page message delivery, Unity's own message processing mechanism can be competent, but the server data layer and UI layer of interaction, is slightly insufficient, because the data is data, UI is Ui,ui is just the presentation of data, So in this unified definition of a set of messaging processing mechanism to handle the UI layer between the page, the client also the interaction between the server



Event mechanism:

1, each event with a unique event ID is identified as EventID, each EventID corresponding to a message processing function, the event ID with a tool class to word processing, the code eventid.as content is as follows:


public sealed class eventid{    //public    const int Eid_weapon_strong = 1;//Weapon Hardening Event ID public    const int eid_wait_s Erver_msg = 2;//Waiting for Server message delivery event ID   }

2, each event sent, may have a certain message data, for example, the server passed over the data, directly thrown to the page, so you need to define a, message data cache class Coreevent.as code as follows


public class Coreevent {protected int m_ieventid = 0;    The message processing code, such as M_ieventcode = 0, indicates that the receiving server message succeeds, M_ieventcode = 1, server interaction fails, when the night game currency is insufficient    //wait, can not complete the equipment hardening functions, UI layer according to different m_ Ieventcode, do the corresponding processing----enhance the success or failure of processing protected int m_ieventcode = 0;    protected Object M_eventparam = null;public coreevent (int eventId, int eventcode = 0) {M_ieventid = Eventid;m_ieventcode = EventCode;}    Public coreevent (int eventId, int eventcode, Object eventparam)    {        m_ieventid = eventId;        M_ieventcode = EventCode;        M_eventparam = Eventparam;    } public int Eventid{get {return m_ieventid;}} public int Eventcode{get {return m_ieventcode;}}    Public Object eventparam    {        get {return m_eventparam;}        set {M_eventparam = value;}}    }


</pre><pre>
3, the following is the main message registration and distribution, the message to remove the core code, file coreeventdispatcher.as, the code is as follows:

public class Coreeventdispatcher {/* message processing delegate, each event ID, corresponds to one or more message handler functions */public delegate void EventHandler (coreevent evt);p R    Ivate dictionary<int, eventhandler> meventhandlerpool = new Dictionary<int, eventhandler> (); /* Message registration function */public void AddEventListener (int eventId, EventHandler handler) {EventHandler Evthandler = null;if ( Meventhandlerpool.containskey (eventId)) {Evthandler = Meventhandlerpool[eventid];evthandler + = handler; Meventhandlerpool[eventid] = Evthandler;}    else Meventhandlerpool.add (eventId, handler); evthandler = null;} /* Message removal function */public void removeeventlistener (int eventId, EventHandler handler) {EventHandler Evthandler = null;if ( Meventhandlerpool.containskey (eventId)) {Evthandler = Meventhandlerpool[eventid];evthandler-= handler; Meventhandlerpool[eventid] = evthandler;if (Evthandler = = null) meventhandlerpool.remove (EVENTID);}    Evthandler = null;} /* Message Dispatch function */public void dispatchcoreevent (Coreevent evt) {if (evt = null && meventhandlerpool.containskey (evt. EventID)) {EventHandler Evthandler = meventhandlerpool[evt. Eventid];if (Evthandler! = null) {Evthandler (EVT);}}}}




At this point, the entire custom processing event has been completed, for convenience, here with an event management Eventmgr class to uniformly manage the custom event message mechanism: The code is as follows

public class Eventmgr {    private static eventmgr ins = null;private Coreeventdispatcher m_eventdispatcher = new Coreeve Ntdispatcher ();    Eventmgr ()    {    } public    static Eventmgr ins    {        get         {if (INS = = null) {                ins = new Eventmgr ();}            return ins;         }    } public void AddEventListener (int eventId, Coreeventdispatcher.eventhandler handler) {m_ Eventdispatcher.addeventlistener (eventId, handler);} public void RemoveEventListener (int eventId, Coreeventdispatcher.eventhandler handler) {m_ Eventdispatcher.removeeventlistener (eventId, handler);} public void Dispatchcoreevent (Coreevent evt) {m_eventdispatcher.dispatchcoreevent (EVT);}}





Custom Interactive events for the server and client or page flow of 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.