当前位置: 首页 > news >正文

MVC基础概念及相应代码示例

(旧的)代码实现方法

        一个功能模块的代码逻辑(显示处理,数据处理,逻辑判定)都写在一起(耦合)

(新的)代码MVC分层实现方法

        显示部分实现(View视图)

        数据处理实现(Model数据模型)

        逻辑判定实现(Controller控制器)

    图片来源:视频课程截图 

MVC是什么:

        C:控制器,负责流程控制和事件响应

        V:视图,负责图形交互

        M:数据模型,负责数据处理

MVC的开发步骤:

        1.页面预制体制作

        2.处理数据(数据模型脚本)

                JSON读写操作

                数据的CURD操作

                        C:Create增加数据

                        U:Update修改数据

                        R:Read读取数据

                        D:Delete删除数据

                根据控制器调用模型的方式数量,在模型中编写对应数量的函数,以供调用

        3.显示(视图脚本)

                文本的显示

                图片的显示

                列表的显示

                其他美术资源(模型,动作特效)

        4.逻辑控制(控制器脚本)

                生命周期函数

                逻辑控制语句

                事件响应

使用MVC分层开发思想的示例代码:

using UnityEngine;
using UnityEngine.UI;
using LitJson;
using System.IO;

//原无MVC开发思想代码
public class UIMainMenu:MonoBehaviour{
    private Text GoldCount;
    public void Start()
    {
        //找到Text子物体
        GoldCount=transform.Find("HeaderCount/Gold/Count").GetComponent<Text>();
        //通过路径找子物体
        transform.Find("HeaderCount/Gold/Add").GetComponent<Button>().onClick.AddListener(GoldAddClick);
        //将JSON文件中的数据,读取出来,显示在页面上
        //进入页面,就检查JSON文件是否存在
        if(!File.Exists(Config.UserNumericalJsonFile))
        {
            //处理JSON数据
            JsonData d = new JsonData();
            d["GoldCount"]=0;
            File.WriteAllText(Config.UserNumericalJsonFile,d.ToJson());
        }
        else
        {
            //将已有的数据读取出来,并显示在UI上
            string json=File.ReadAllText(Config.UserNumericalJsonFile)
            JsonData d=JsonMapper.ToObject(json);
            GoldCount.text=d["GoldCount"].ToString();
        }
    }
    public void GoldAddClick(){
        //读取数据
        string json=File.ReadAllText(Config.UserNumericalJsonFile);
        JsonData d = JsonMapper.ToObject(json);
        //增加金币数1
        d["GoldCount"]=(int)d["GoldCount"]+1;
        //更新显示部分(Text)
        GoldCount.text=d["GoldCount"].ToString();
        //更新JSON数据(JSON)
        File.WriteAllText(Config.UserNumericalJsonFile,d.ToJson());
    }
} 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class Config
{
    public static string UserNumericalJsonFile
        =Application.persistentDataPath+"/UserNumerical.json"; 
}

    
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;

//模型层
public static class UserDataModel
{
    //创建新的用户数据
    public static void CreateNew()
    {
         if(!File.Exists(Config.UserNumericalJsonFile))
        {
            //处理JSON数据
            JsonData d = new JsonData();
            d["GoldCount"]=0;
            File.WriteAllText(Config.UserNumericalJsonFile,d.ToJson());
        }
    }
    //读取用户数据
    public static JsonData ReadAllData()
    {
         //将已有的数据读取出来,并显示在UI上
         string json=File.ReadAllText(Config.UserNumericalJsonFile)
         return JsonMapper.ToObject(json);
    }
    //更新金币数
    public static JsonData UpdateGold(int count)
    {
         //获取原来json文件中的值
        string json=File.ReadAllText(Config.UserNumericalJsonFile);
        JsonData d = JsonMapper.ToObject(json);
        d["GoldCount"]=(int)d["GoldCount"]+1;
        File.WriteAllText(Config.UserNumericalJsonFile,d.ToJson());
        return d;
    }
using Sytem.Collections;
using Sytem.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using LitJson;

//控制器层
public class UIMainMenuController:MonoBehaviour
{
    private UserNumericalView View;
    public void Start()
    {
        View=transform.Find("HeaderCount").GetComponent<UserNumericalView>();
        View.Init();
        //通过路径找子物体
        transform.Find("HeaderCount/Gold/Add").GetComponent<Button>().onClick.AddListener(GoldAddClick);
        UserDataModel.CreateNew();
        JsonData d = UserDataModel.ReadAllData();
        View.Refresh(d);
    }
    public void GoldAddClick()
    {
        JsonData d = UserDataModel.UpdateGold(1);
        View.Refresh(d);
    }
}
        
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using LitJson;

//视图层
public class UserNumericalView:MonoBehaviour
{
    private Text GoldCount;
    public void Init()
    {
        //找到Text子物体
        GoldCount=transform.Find("Gold/Count").GetComponent<Text>();
    }
    public void Refresh(JsonData data)
    {
        GoldCount.text=data["GoldCount"].ToString();
    }
}

以上代码仅供帮助理解MVC开发思想,未提供对应的UI各物体挂载及摆放,如想实践,自取代码进行相应修改调试 。

该系列专栏为网课课程笔记,仅用于学习参考。    

相关文章:

  • Nextjs15 - middleware的使用
  • LeetCode算法题(Go语言实现)_11
  • (每日一道算法题) K 个一组翻转链表
  • 2025宠物智能喂养设备全栈开发方案:Python驱动的高效实现路径
  • IntelliJ IDEA创建Maven工程
  • Ardupilot开源无人机之Geek SDK进展2025Q2
  • 我的世界1.20.1forge模组进阶开发教程——结构(3)
  • 华为 eNSP 链路聚合:从面试问题到实战解析
  • MySQL数据库精研之旅第四期:解锁库操作高阶技能
  • Python数据可视化与数据处理全解析:Matplotlib图形控制与Pandas高效数据分析实战
  • MySQL中的Redo Log、Undo Log和Binary Log
  • 大模型在支气管肺癌预测及临床决策中的应用研究报告
  • 蓝桥杯 临时抱佛脚 之 二分答案法与相关题目
  • 【设计模式】抽象工厂模式(含与工厂方法模式的对比)
  • Unity-RectTransform设置UI width
  • wokwi arduino mega 2560 - 模数与数模转换AD和DA
  • 设置git拉取的文件换行格式
  • Codeforces Round 1013 (Div. 3)
  • 企业入驻成都国际数字影像产业园,可享150多项专业服务
  • AwesomeQt分享(含源码)
  • 经济日报:降准降息,提前还房贷划算吗?
  • 大学2025丨专访清华教授沈阳:建议年轻人每天投入4小时以上与AI互动
  • 起底新型保健品电话销售诈骗:从快递信息中筛选对象,忽悠其高价买药
  • 哈马斯官员:进一步停火谈判毫无意义
  • 多省份晒出“五一”旅游“成绩单”:北京游客接待量、旅游消费创历史新高
  • 贵州黔西市游船倾覆事故发生后,多家保险公司紧急响应