Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Simple Application of MVC in Unity

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article briefly introduces the simple application of the MVC framework in Unity. MVC has been widely used since it was designed in 1982, especially when software and games are iterated so fast. Efficient add and delete function low coupling and compact flexible framework MVC, deeply loved by the majority of ProgramDesigner.

Data is the soul of the program, the view is to see the eyes of the soul, and the controller removes the fog from it, which forms the present MVC.

Simple example: Model

Using UnityEngine

Using System.Collections

/ / /

/ / Model delegation (executed when user information changes)

/ / /

Public delegate void OnValueChange (int val)

Public class PlayerMsgModel

{

/ / player level

Private int playerLevel

/ / player experience

Private int playerExperience

/ / player upgrade experience

Private int playerFullExperience

/ / number of gold coins

Private int goldNum

/ / declare the delegate object and receive the events triggered when the level changes

Public OnValueChange OnLevelChange

/ / declare the delegate object and receive the events triggered when the experience changes

Public OnValueChange OnExperienceChange

/ / declare the delegate object and receive the events triggered when the upgrade experience changes

Public OnValueChange OnFullExperienceChange

/ / declare the delegate object and receive the events triggered when the number of gold coins changes

Public OnValueChange OnGoldNumChange

/ / singleton

Private static PlayerMsgModel mod

Public static PlayerMsgModel GetMod ()

{

If (mod = = null) {

Mod = new PlayerMsgModel ()

}

Return mod

}

Private PlayerMsgModel ()

{

}

/ / /

/ / player level attribute

/ / /

/ The player level.

Public int PlayerLevel {

Get {

Return playerLevel

}

Set {

PlayerLevel = value

/ / if the delegate object is not empty

If (OnLevelChange! = null) {

/ / execute the delegation

OnLevelChange (playerLevel)

}

}

}

/ / /

/ / player experience attribute

/ / /

/ The player experience.

Public int PlayerExperience {

Get {

Return playerExperience

}

Set {

PlayerExperience = value

If (OnExperienceChange! = null) {

OnExperienceChange (playerExperience)

}

}

}

/ / /

/ / player upgrade experience attribute

/ / /

/ The player full experience.

Public int PlayerFullExperience {

Get {

Return playerFullExperience

}

Set {

PlayerFullExperience = value

If (OnFullExperienceChange! = null) {

OnFullExperienceChange (playerFullExperience)

}

}

}

/ / /

/ / Gold Coin quantity attribute

/ / /

/ The gold number.

Public int GoldNum {

Get {

Return goldNum

}

Set {

GoldNum = value

If (OnGoldNumChange! = null) {

OnGoldNumChange (goldNum)

}

}

}

}

Simple example: View

Using UnityEngine

Using System.Collections

Using UnityEngine.UI

Public class PlayerMsgView: MonoBehaviour

{

/ / UI

Public Text playerLevel

Public Text playerExperience

Public Text goldNum

Public Button experienceUpButton

Void Start ()

{

/ / delegate event binding

PlayerMsgModel.GetMod () .OnLevelChange + = SetLevel

/ / delegate event binding

PlayerMsgModel.GetMod () .OnExperienceChange + = SetExperience

PlayerMsgModel.GetMod () .OnFullExperienceChange + = SetFullExperience

PlayerMsgModel.GetMod () .OnGoldNumChange + = SetGoldNum

/ / View bind button control function

ExperienceUpButton.onClick.AddListener (

PlayerMsgController.controller.OnExperienceUpButtonClick)

}

/ / modify the UILevelvalue

Public void SetLevel (int level)

{

PlayerLevel.text = level.ToString ()

}

/ / modify UI experience value

Public void SetExperience (int experience)

{

/ / detach the string with "/"

String [] str = playerExperience.text.Split (new char [] {'/'})

/ / reorganize with new experience values

PlayerExperience.text = experience + "/" + str [1]

}

Public void SetFullExperience (int fullExiperience)

{

String [] str = playerExperience.text.Split (new char [] {'/'})

PlayerExperience.text = str [0] + "/" + fullExiperience

}

Public void SetGoldNum (int goldn)

{

GoldNum.text = goldn.ToString ()

}

}

Simple example: Controller

Using UnityEngine

Using System.Collections

Public class PlayerMsgController: MonoBehaviour

{

Public static PlayerMsgController controller

Private int levelUpValue = 20

Void Awake ()

{

Controller = this

}

Void Start ()

{

PlayerMsgModel.GetMod () .PlayerLevel = 1

PlayerMsgModel.GetMod () .PlayerExperience = 0

PlayerMsgModel.GetMod (). PlayerFullExperience = 100

PlayerMsgModel.GetMod () .GoldNum = 0

}

/ / /

/ / enhance experience button click event

/ / /

Public void OnExperienceUpButtonClick ()

{

PlayerMsgModel.GetMod () .PlayerExperience + = levelUpValue

If (PlayerMsgModel.GetMod (). PlayerExperience

> = PlayerMsgModel.GetMod () .PlayerFullExperience) {

PlayerMsgModel.GetMod () .PlayerLevel + = 1

PlayerMsgModel.GetMod () .PlayerFullExperience + =

200 * PlayerMsgModel.GetMod () .PlayerLevel

LevelUpValue + = 20

If (PlayerMsgModel.GetMod () .PlayerLevel% 3 = = 0) {

PlayerMsgModel.GetMod () .GoldNum + =

100 * PlayerMsgModel.GetMod () .PlayerLevel

}

}

}

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report