首先我们先谈Unity3D本身的MonoBehavior脚本。
MonoBehavior我们可以将他理解为界面层与界面直接沟通的上层脚本,在他底部的控制,逻辑,数据等有必要用MonoBehavior脚本么?如果在unity中我们进行开发时如果滥用MonoBehavior脚本,无疑会造成相当高的耦合度,并且当我们对程序进行迭代时会变得困难重重。所以在这里介绍MVC框架来对代码进行解耦。
我们以创建一个玩家角色面板来演示
1.简单实例-Model
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UserModel {
private int currentExp;
private int fullExp;
private int cone;
private int level;
public UserModel()
{
level = 1;
cone = 0;
fullExp = 100;
currentExp = 0;
}
#region 字段封装
public int CurrentExp
{
get
{
return currentExp;
}
set
{
currentExp = value;
}
}
public int FullExp
{
get
{
return fullExp;
}
set
{
fullExp = value;
}
}
public int Cone
{
get
{
return cone;
}
set
{
cone = value;
}
}
public int Level
{
get
{
return level;
}
set
{
level = value;
}
}
#endregion
}
2.简单实例-View
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UserView : MonoBehaviour {
private Text levelText;
private Text coneText;
private Text currentExp;
private Text fullExp;
[HideInInspector]
public Button addExp;
[HideInInspector]
public Button addCone;
private void Awake()
{
levelText = GameTool.FindTheChild(gameObject, "levelText").gameObject.GetComponent<Text>();
coneText = GameTool.FindTheChild(gameObject, "coneText").gameObject.GetComponent<Text>();
currentExp = GameTool.FindTheChild(gameObject, "currentExp").gameObject.GetComponent<Text>();
fullExp = GameTool.FindTheChild(gameObject, "fullExp").gameObject.GetComponent<Text>();
addExp = GameTool.FindTheChild(gameObject, "addExp").gameObject.GetComponent<Button>();
addCone = GameTool.FindTheChild(gameObject,"addCone").gameObject.GetComponent<Button>();
}
public void SetConeText(int cone)
{
coneText.text = cone.ToString();
}
public void SetFullExp(int fullexp)
{
this.fullExp.text = fullexp.ToString();
}
public void SetCurrentExp(int currentexp)
{
this.currentExp.text = currentexp.ToString();
}
public void SetLevelText(int level)
{
this.levelText.text = level.ToString();
}
private void Start()
{
}
}
3.简单实例-Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UserController : MonoBehaviour {
private UserView view;
private UserModel userModel;
private void Awake()
{
view = gameObject.GetComponent<UserView>();
userModel = new UserModel();
}
public void AddExp(GameObject go)
{
userModel.CurrentExp += 99;
if (userModel.CurrentExp>=userModel.FullExp)
{
userModel.CurrentExp = userModel.CurrentExp - userModel.FullExp;
LevelUP();
}
view.SetCurrentExp(userModel.CurrentExp);
view.SetFullExp(userModel.FullExp);
view.SetLevelText(userModel.Level);
}
public void AddCone(GameObject go)
{
userModel.Cone += 1000;
view.SetConeText(userModel.Cone);
}
public void LevelUP()
{
userModel.Level++;
userModel.FullExp = userModel.Level * 100;
}
// Use this for initialization
void Start () {
UGUIEventListener.Get(view.addCone.gameObject).onClick += AddCone;
UGUIEventListener.Get(view.addExp.gameObject).onClick += AddExp;
}
// Update is called once per frame
void Update () {
}
}
代码中使用到了两个简单的帮助类,很简单在其他博文中也介绍过,就不在介绍了。
最后附上演示代码链接