As a large game, it is unavoidable to use complex interfaces, network message processing, data caching and other slightly complex things. In fact, we all know that for a hand tour, the huge system engineering below, is actually a lot of basic technology stack. Therefore, in the game development, the emergence of various types of bugs is not because of technical implementation problems, but from the framework, code management of the design errors appear.
Reprint please indicate source article from Http://blog.csdn.net/huutu QQ790621656 http://www.thisisgame.com.cn
In view of the above problems, many software designers have made efforts to design a lot of logical and clear framework pattern structure, here I introduce the MVC design pattern used in our project.
Reprint please indicate source article from Http://blog.csdn.net/huutu QQ790621656 http://www.thisisgame.com.cn
The concept of MVC
The concept of referencing MVC:
The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.
Reprint please indicate source article from Http://blog.csdn.net/huutu QQ790621656 http://www.thisisgame.com.cn
Why use MVC in Unity3d?
Here we have a login interface (GIF diagram please be patient to load), we click Log in, we need to network contract to the server. After the server receives the packet, the account password is compared. Then return the data to the client, and then the client pops up prompting box to prompt for the result.
Single-Logical Client mode
From a simple logic level, we can do this in the following scenario.
The single-logical client-mode, network logic, and UI events are all handled in the UI logic processing class.
For the above login logic, we have the following Class
Loginwindow-Handle interface Click, swipe, input events and register network callback, processing network messages.
Ccnetworkclient--Responsible for send, Receive network data and network data distribution.
In a simple project, this framework scheme is not intended to be the most suitable, saving time and effort, a glance can understand.
However, in large-scale games and projects, the code is too large, with a single logic of the client model, management of the weak, not good maintenance. So we introduced the MVC pattern
Reprint please indicate source article from Http://blog.csdn.net/huutu QQ790621656 http://www.thisisgame.com.cn
The MVC framework pattern in Unity3d
In Unity3d, there are two ways to notify View by model, using SendMessage and using delegate. This leads to the client MVC framework model based on network usage SendMessage and network using delegate.
For the MVC framework pattern, we have the following class
Loginwindow--Responsible for UI interface Event issues an updated view.
Loginmodel--Responsible for registering network callbacks and handling network messages.
Ccnetworkclient--Responsible for send, Receive network data and network data distribution.
Reprint please indicate source article from Http://blog.csdn.net/huutu QQ790621656 http://www.thisisgame.com.cn
Client MVC pattern based on network using SendMessage
Frame mode such as
Reprint please indicate source article from Http://blog.csdn.net/huutu QQ790621656 http://www.thisisgame.com.cn
After the network data (Receivenetworkpacket) is received, networks (Client) is processnetworkevent for data distribution. The network event (Registernetworkevent) that was registered in the previous model is then processed.
If you need to update the UI, use Gameobject.sendmessage to notify the UI update.
Reprint please indicate source article from Http://blog.csdn.net/huutu QQ790621656 http://www.thisisgame.com.cn
Client MVC framework model based on network using delegate
For a ARPG game, the smoothness of the game determines the studio revenue to a certain extent. The optimization of running speed not only from memory, CPU, GPU, to own logic coding, Atlas use and so on analysis, to the Unity System API selection also to optimize consideration.
Gameobject.sendmessage () is a less efficient API, and we use Delegate to replace it.
Reprint please indicate source article from Http://blog.csdn.net/huutu QQ790621656 http://www.thisisgame.com.cn
Introduce delegate.
When the model sends the network data, the registered uievent is also sent to the server. The server returns as is.
When the model receives the data that needs to update the UI interface, it is updated with the previously registered uieventdelegate.
Reprint please indicate source article from Http://blog.csdn.net/huutu QQ790621656 http://www.thisisgame.com.cn
Unity3d client-side MVC framework model based on network usage SendMessage and Network usage delegate (a)