Unity——MVC

MVC

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

        V:视图,负责图形交互

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

MVC的开发步骤

        1.页面预制体的制作

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

                JSON的读写操作

                数据的CURD操作

                        C:Create增加操作

                        U:Update修改数据

                        R:Read读取数据

                        D:Delete删除数据

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

        3.显示(视图脚本)

                文本的显示

                图片的显示

                列表的显示

                其他美苏资源

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

                生命周期函数

                逻辑控制语句

                事件响应                    

处理数据

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

public class UserDataModel
{
    /// <summary>
    /// 创建新的用户数据
    /// </summary>
   public static void CreateNew()
    {
        if (!File.Exists(Config.UserNumbericalJsonFile))
        {
            //处理Json数据
            JsonData data = new JsonData();
            data["GoldCount"] = 0;
            File.WriteAllText(Config.UserNumbericalJsonFile, data.ToJson());
        }       
    }

    /// <summary>
    /// 读取json文件里面的内容,并且返回
    /// </summary>
    /// <returns></returns>
    public static JsonData ReadAllData()
    {
        //读取数据
        string json = File.ReadAllText(Config.UserNumbericalJsonFile);
       return JsonMapper.ToObject(json);
    }

    /// <summary>
    /// 添加count金币,并且上传到文件
    /// </summary>
    /// <param name="count"></param>
    public static JsonData UpdateGold(int count)
    {
        //读取数据
        string json = File.ReadAllText(Config.UserNumbericalJsonFile);
        JsonData data = JsonMapper.ToObject(json);

        //增加金币
        data["GoldCount"] = (int)data["GoldCount"] + count;
      
        //更新json数据
        File.WriteAllText(Config.UserNumbericalJsonFile, data.ToJson());

        return data;
    }
}

显示数据

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

public class UserNumericalView : MonoBehaviour
{
    private Text GoldCount;

    /// <summary>
    /// UI显示
    /// </summary>
    public void Init()
    {
        //找到Text字体
        GoldCount = transform.Find("Gold/Count").GetComponent<Text>();
    }

    public void Refresh(JsonData data)
    {
        GoldCount.text = data["GoldCount"].ToString();
    }
}

逻辑控制层

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

public class UIMainMenuController : MonoBehaviour
{
    private UserNumericalView view;

    void Start()
    {      
        view = transform.Find("HeaderCount").GetComponent<UserNumericalView>();
        view.Init();

        //通过路劲找子物体,添加按下事件
        transform.Find("HeaderCount/Gold/Add").GetComponent<Button>().onClick.AddListener(GoldAddClick);

        //调用静态方法创建新用户表格
        UserDataModel.CreateNew();

        //读取所有的文件
        JsonData data = UserDataModel.ReadAllData();
        view.Refresh(data);
    }

    private void GoldAddClick()
    {
        JsonData data = UserDataModel.UpdateGold(1);
        view.Refresh(data);
    }
}

猜你喜欢

转载自blog.csdn.net/m0_51743362/article/details/123898771