Home > Web Front-end > HTML Tutorial > Custom interaction events between server and client or page flow in unity game development_html/css_WEB-ITnose

Custom interaction events between server and client or page flow in unity game development_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:51:42
Original
956 people have browsed it

Introduction:

1. Interaction between game pages and pages

(1) Equipment backpack


(2 ) Equipment information page





In game development , sometimes there is indispensable correlation between 2D pages, such as the backpack system of the game. The player enters the backpack system (see the equipment backpack and equipment information page in the picture above), clicks on the equipment to view the equipment information page, which is generally the information page of card games. All come with the function of increasing the attribute value of equipment

, such as the enhancement function of equipment in "Release the Three Kingdoms". After the player spends a certain amount of game currency to enhance the equipment, the equipment attributes in the equipment information page will change accordingly. refresh. However, after closing the equipment information page, the corresponding equipment description on the backpack system

page will also change. But how does the backpack system know that the original attribute values ​​​​have been changed in the equipment information page? Then this The registration, monitoring and dispatching of events are used. In other words, if the equipment has been strengthened

, when closing the equipment information page, a message must be sent to the backpack system page, and the backpack system has registered a message processing function when it is opened to handle the sending. Incoming messages, when

of the message sent from the information page is received, the backpack system page is refreshed in the message processing function. This completes the consistency of the data


2. Interaction between client and server

As mentioned above, the equipment enhancement function is involved. In the interaction between the client and the server, when the player clicks the enhancement button, he or she needs to send an enhancement request to the server. Before sending, the client needs to register a message processing function. After the server enhancement is successful, the receiving server sends the enhancement request to the client. After receiving the message of success or failure, the client will perform enhanced success special effects and refresh the page.


Both of the above two situations involve the interactivity of game elements. Among them, unity’s own message processing mechanism can handle the message transmission between pages, but the server The interaction between the data layer and the UI layer is slightly insufficient, because data is data, UI is UI, and UI is just a display of data. Therefore, a set of message passing processing mechanisms are defined here to process between UI layer pages. Interaction between client and server



Event mechanism:

1, each event uses a unique event The ID identifier is EventID. Each EventID corresponds to a message processing function. The event ID is processed by a tool class. The content of the code EventID.as is as follows:


public sealed class EVENTID{    //    public const int EID_WEAPON_STRONG = 1;//武器强化事件ID    public const int EID_WAIT_SERVER_MSG = 2;//等待服务器消息传送事件ID   }
Copy after login

2. Each event sent may contain certain message data. For example, the data passed by the server is thrown directly to the page, so it is necessary to define a message data cache class CoreEvent.as The code is as follows


public class CoreEvent {	protected int m_iEventId = 0;    //消息处理码,比如m_iEventCode = 0,表示接受服务器消息成功,m_iEventCode = 1,服务器交互失败,当晚间游戏货币不足的时    //候,不能完成装备强化等功能,UI层根据不同的m_iEventCode,做相应的处理----强化成功或者失败的处理	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; }    }}
Copy after login


Copy after login
3. The following is the main message registration and dispatch, message removal core code, file CoreEventDispatcher.as, the code is as follows:

public class CoreEventDispatcher {    /*消息处理委托,每一个事件id,对应一个或多个消息处理函数*/	public delegate void EventHandler(CoreEvent evt);	private Dictionary<int, EventHandler> mEventHandlerPool = new Dictionary<int, EventHandler>();    /*消息注册函数*/	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;	}    /*消息移除函数*/	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;	}    /*消息派发函数*/    public void DispatchCoreEvent(CoreEvent evt)	{		if(evt != null && mEventHandlerPool.ContainsKey(evt.EventID))		{			EventHandler evtHandler = mEventHandlerPool[evt.EventID];			if( evtHandler != null)			{				evtHandler(evt);			}		}	}}
Copy after login




At this point, the entire custom event processing has been completed. For convenience, an event management EventMgr class is used 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 CoreEventDispatcher();    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);	}	}
Copy after login





source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template