This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plugin sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

Hello everyone, I am a Buddhist engineer ☆ quiet small magic dragon ☆, update Unity development skills from time to time, think useful remember one key three link oh.

One, foreword

MVC is a common design framework, which divides the functions of code. The main principle is to separate software user interface from business logic, so as to strengthen the expansibility, reusability, maintainability and flexibility of code. MVC is widely used in desktop applications and web architecture, so how to apply it to Unity3d? Let’s take you to understand this design framework and how to apply it in Unity.

Ii. Introduction to MVC

Introduction to the

The full name of MVC is Model View Controller, which is the abbreviation of Model, View and Controller. It is a Model of software design. It organizes code by separating business logic, data and interface display, and gathers business logic into one component. There is no need to rewrite business logic while improving and customizing interfaces and user interactions. MVC has been uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure.

The Model is the part of the application that handles the application’s data logic. Typically, model objects are responsible for accessing data in a database. A View is the part of the application that handles the display of data. Views are usually created from model data. A Controller is the part of the application that handles user interaction. Typically the controller is responsible for reading data from the view, controlling user input, and sending data to the model. MVC began to exist in desktop applications. M refers to the business model, V refers to the user interface, and C refers to the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same application can use different forms of expression. For example, a batch of statistical data can be represented by bar charts and pie charts respectively. The purpose of C is to ensure the synchronization of M and V. Once M changes, V should be updated synchronously.

Model-view-controller (MVC) is a software design pattern invented by Xerox PARC in the 1980s for the programming language Smalltalk-80, which has been widely used. It has since been recommended as a design pattern for Oracle’s Sun Java EE platform and is increasingly popular with developers using ColdFusion and PHP. The model-View-Controller pattern is a useful toolbox that has many benefits, but also some drawbacks.

MVC business process

Is MVC a design pattern

MVC is a design pattern, but it is not in the 23 design patterns summarized by Gof, so it is not exactly a design pattern, but a combination of multiple design patterns, not just a design pattern. MVC consists of three modes: combination mode, strategic role mode and observer mode. The power of MVC in software development cannot be separated from the tacit cooperation of these three modes. For programmers who believe that design patterns are useless, know that as long as you use MVC, you can’t live without design patterns. The composite pattern is only active in the view layer, and the implementation of the view layer uses the composite pattern. Of course, the implementation here is the low-level implementation, which is done by the programming framework vendor, not the ordinary programmer.

The class hierarchy of the composite pattern is a tree, whereas when we do the Web the view layer is the HTML page, the HTML structure is the tree, so this is actually a composite pattern application, but the browser manufacturers have taken the interface work out for us, but it’s really part of the MVC application, We just don’t feel it, which is why we feel that View is the easiest and most unambiguous layer to implement.

In addition to the web page other user interface programs, such as WPF, Android, ASP.NET and so on are the use of tree structure to organize the interface control objects, because the composite mode is from the general solution of interface design is always extracted. So it’s not so much that MVC has chosen the composite pattern as that the composite pattern has to exist in MVC because it has to exist as long as the user interface is involved. In fact, even if the programmer does not understand the composite pattern, it does not affect the correct use of MVC, the composite pattern exists in the programmer’s inaccessible position.

However, the observer pattern and the policy pattern are the more important parts of the real MVC.

The observer mode consists of two parts, the object being observed and the observer, also known as the listener. Corresponding to MVC, Model is the object being observed and View is the observer. Once the Model layer changes, the View layer is notified to update. The View layer and the Model layer hold references to each other. When we develop Web MVC applications, the HTML of the view layer and the business logic of the Model layer are separated by an HTTP, so they cannot be associated explicitly, but their relationship between observer and listener does not change. When the View submits data to the server via HTTP, the Model on the server receives the data to perform some action, and sends the result back to the View via HTTP response. The View (browser) receives the data update interface, which is just another form of the observer mode.

However, apart from the Web, when a PURE MVC structure is expressed through code, there is no doubt that the relationship between View and Model is observer and observed, based on the observer Model theory. Even if the actual implementation is a little out of shape on the Web because of HTTP barriers, the core principles and philosophy remain the same.

Finally, there is the strategic pattern. The strategy mode is the relationship between View and Controller, Controller is a strategy of View, Controller is replaceable for View, the relationship between View and Controller is one-to-many, in the actual development scenario, It is also common to see a View referenced by multiple controllers, which is an embodiment of the strategy pattern, but not as intuitive.

To summarize, the design pattern that corresponds to the relationship between MVC layers

View layer, a separate implementation of the composite pattern

The Model layer and the View layer implement the observer pattern

The View layer and the Controller layer implement the strategy mode

MVC uses these three design patterns together. Understand these three design patterns and THERE is no mystery to MVC. If you do not know these three design patterns to learn MVC, no matter how you learn it, you will always have a half-knowledge, and there will be no problems when you use it.

Third, refer to the article

Deep understanding of MVC blog.csdn.net/zuiyingong6… Introduction of the MVC www.cnblogs.com/diyunfei/p/… 【Unity】MVC framework blog.csdn.net/qq_39574690… The Unity of the simple MVC application www.jianshu.com/p/acc689be5… Unity3D 】 【 mobile game development practices “tencent billiards” client development experience blog.csdn.net/q764424567/… The MVC framework baike.baidu.com/item/MVC%E6…

4. Use MVC in Unity

This article is mainly about how I use MVC pattern in Unity. MVC pattern cannot be copied into Unity, so many things have been modified to suit my project. If there is anything wrong, I hope you can point out.

Using the principle of

This is the structure of the code document. As far as I know, there are only two modes of MVC application in Unity. One is to divide according to business functions first, and then according to MVCCake Talk games are based on this model, which makes the modules more focused (highly cohesive). The second model is to divide the modules according to MVC first, and then divide the business functionsThe second method is often used and found to be less maintainable than the first method as the number of operational modules increases.

The key of MVC lies in business division and code separation. In fact, it is similar to three-tier architecture, which separates business layer, logic layer and data layer and calls each other through an interface, but we don’t know how the interface is implemented.

So the question now is how to divide.

Implementation: Model (Model layer) : This layer is mainly used to store user data, UI data, static fields, data storage, and the storage of Model map resources, such as:

  • Storing static fields

  • To store data

  • Holds the model material field

  • Database add delete change check operation

The View layer is where you put UI parameters, get UI data, get button events, etc Click on the event

Controller (control layer) this layer is to implement the business logic function, get Model data, notify the View layer to update data, connecting the previous and the next functions

Five, the instance

Untiy use MVC pattern to realize the function of replacing pictures step 1: design interface Step 2: MVC builds model_main.cs View_Main.cs Controller_Main.cs

As for the method of the mouse click event, I talked about it in another article. If you don’t understand it, go to that post and take a closer look.Blog.csdn.net/q764424567/…Effect:The source code has been uploaded to CSDN for download on demandDownload.csdn.net/download/q7…

Six, summarized

PS: The most important thing for programmers is to summarize, or learn more to the critical moment is useless, summarize and comb, and then write down, when you forget, you can come back to see, there will be a different understanding oh. The application of MVC pattern in Unity has been explained. MVC pattern is actually a combination of multiple design patterns and an improved product of three-tier architecture. The basic principle is to separate business logic, function realization and data, which is conducive to adding functions and maintaining. As the saying goes, there are a thousand Hamlets in a thousand people. After everyone studies MVC, the methods used in Unity are not the same, and the methods are also diverse. I mainly provide an idea and train of thought, and we need to use our brains more to optimize the application. Finally, a review of the Model (data layer) : static fields, data stores, Model resource stores. View layer: the layer that the user can see, refers to the UI that can be seen, the Model, loading and calling events Controller (management layer) : implementing business logic functions, loading Model resources, function implementation, etc