search
HomeWeb Front-endHTML TutorialCustom interaction events between server and client or page flow in unity game development_html/css_WEB-ITnose

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   }

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; }    }}



 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);			}		}	}}</int></int>




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);	}	}





Statement
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
What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.