浅谈MVC在Unity中的使用

首先我们先谈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 () {
		
	}
}

代码中使用到了两个简单的帮助类,很简单在其他博文中也介绍过,就不在介绍了。

最后附上演示代码链接


  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值