浅谈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
    评论
Unity实现MVC(Model-View-Controller)通常需要以下步骤: 1. 创建模型(Model):在Unity,模型通常是指游戏对象及其组件,例如,一个角色、一个道具等。为每个模型创建一个脚本,该脚本包含模型的数据和操作方法。 2. 创建视图(View):视图是指模型的可视化表示,例如,游戏场景、UI界面等。在Unity,可以使用Prefab和场景来创建视图。视图通常会访问模型的数据,但不会直接修改它。 3. 创建控制器(Controller):控制器是模型和视图之间的介者,它负责处理用户输入、更新模型数据以及更新视图。在Unity,可以为每个游戏对象或UI元素创建一个控制器脚本,该脚本包含响应用户输入的方法以及更新模型和视图的方法。 4. 将模型、视图和控制器连接起来:在Unity,可以使用事件或委托来实现模型、视图和控制器之间的通信。例如,当用户点击一个按钮时,控制器会调用模型的方法来更新数据,并调用视图的方法来更新UI界面。 5. 维护数据的一致性:在MVC模式,数据的一致性非常重要。因此,在Unity实现MVC时,需要确保模型、视图和控制器之间的数据同步。例如,当模型数据发生变化时,需要及时更新视图和控制器的相应数据。 以上是在Unity实现MVC的一般步骤,具体实现方式可以根据项目需求和开发团队的实际情况来进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值