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

Jun 24, 2016 am 11:51 AM

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





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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles