15.MVVM的Model数据层和主面板Panel面板层

15.MVX框架-MVVM框架-主面板Panel面板层


15.1 知识点

导入BaseFramwork基础框架,数据层不需要修改,直接拿普通的MVC中的数据层就行

定义MVVM_MainPanel,这个类继承自BaseUGUIPanel,可以理解为是界面层和逻辑层的融合。

初始化更新面板,并添加数据更新时的监听

public override void Init()
{
    // 第一次打开时更新
    UpdateInfo(MVVM_PlayerModel.Data);

    // 添加数据更新事件监听
    MVVM_PlayerModel.Data.AddEventListener(UpdateInfo);
}

处理按钮点击事件,根据按钮名称执行相应的逻辑。

protected override void OnButtonInitClick(string btnName)
{
    base.OnButtonInitClick(btnName);
    switch (btnName)
    {
        case "btnRole":
            // 处理角色面板打开
            BaseUGUIManager.Instance.ShowPanel<MVVM_RolePanel>(BaseUGUIManager.UI_SAVE_PATH + "MVVM/");
            break;
    }
}

定义方法更新界面上的文本信息,根据传入的数据更新相应的 UI 元素。

public void UpdateInfo(MVVM_PlayerModel data)
{
    // 直接在这获取控件进行更新
    GetUIElement<Text>("txtName").text = data.PlayerName;
    GetUIElement<Text>("txtLev").text = "LV." + data.Lev;

    GetUIElement<Text>("txtMoney").text = data.Money.ToString();
    GetUIElement<Text>("txtGem").text = data.Gem.ToString();
    GetUIElement<Text>("txtPower").text = data.Power.ToString();
}

销毁时移除监听

private void OnDestroy()
{
    // 移除数据更新事件监听
    MVVM_PlayerModel.Data.RemoveEventListener(UpdateInfo);
}

添加主入口进行测试

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

public class MVVMMain : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            //显示主面板
            BaseUGUIManager.Instance.ShowPanel<MVVM_MainPanel>(BaseUGUIManager.UI_SAVE_PATH + "MVVM/");
        }
        else if (Input.GetKeyDown(KeyCode.N))
        {
            //隐藏主面板
            BaseUGUIManager.Instance.HidePanel<MVVM_MainPanel>();
        }
    }
}

15.2 知识点代码

MVVM_PlayerModel

using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// 作为一个唯一的数据模型 一般情况 要不自己是个单例模式对象
/// 要不自己存在在一个单例模式对象中
/// </summary>
public class MVVM_PlayerModel
{
    //数据内容
    private string playerName;
    public string PlayerName
    {
        get
        {
            return playerName;
        }
    }
    private int lev;
    public int Lev
    {
        get
        {
            return lev;
        }
    }
    private int money;
    public int Money
    {
        get
        {
            return money;
        }
    }
    private int gem;
    public int Gem
    {
        get
        {
            return gem;
        }
    }
    private int power;
    public int Power
    {
        get
        {
            return power;
        }
    }
    private int hp;
    public int HP
    {
        get
        {
            return hp;
        }
    }
    private int atk;
    public int Atk
    {
        get
        {
            return atk;
        }
    }
    private int def;
    public int Def
    {
        get
        {
            return def;
        }
    }
    private int crit;
    public int Crit
    {
        get
        {
            return crit;
        }
    }
    private int miss;
    public int Miss
    {
        get
        {
            return miss;
        }
    }
    private int luck;
    public int Luck
    {
        get
        {
            return luck;
        }
    }


    //通知外部更新的事件
    //通过它和外部建立联系 而不是直接获取外部的面板
    private event UnityAction<MVVM_PlayerModel> updateEvent;

    //在外部第一次获取这个数据 如何获取
    //通过单例模式 来达到数据的唯一性 和数据的获取
    private static MVVM_PlayerModel data = null;

    public static MVVM_PlayerModel Data
    {
        get
        {
            if( data == null )
            {
                data = new MVVM_PlayerModel();
                data.Init();
            }
            return data;
        }
    }

    //数据相关的操作

    // 初始化
    public void Init()
    {
        playerName = PlayerPrefs.GetString("PlayerName", "韬老狮");
        lev = PlayerPrefs.GetInt("PlayerLev", 1);
        money = PlayerPrefs.GetInt("PlayerMoney", 9999);
        gem = PlayerPrefs.GetInt("PlayerGem", 8888);
        power = PlayerPrefs.GetInt("PlayerPower", 99);

        hp = PlayerPrefs.GetInt("PlayerHp", 100);
        atk = PlayerPrefs.GetInt("PlayerAtk", 20);
        def = PlayerPrefs.GetInt("PlayerDef", 10);
        crit = PlayerPrefs.GetInt("PlayerCrit", 20);
        miss = PlayerPrefs.GetInt("PlayerMiss", 10);
        luck = PlayerPrefs.GetInt("PlayerLuck", 40);
    }

    // 更新 升级
    public void LevUp()
    {
        //升级 改变内容
        lev += 1;

        hp += lev;
        atk += lev;
        def += lev;
        crit += lev;
        miss += lev;
        luck += lev;

        //改变过后保存
        SaveData();
    }

    // 保存 
    public void SaveData()
    {
        //把这些数据内容 存储到本地
        PlayerPrefs.SetString("PlayerName", playerName);
        PlayerPrefs.SetInt("PlayerLev", lev);
        PlayerPrefs.SetInt("PlayerMoney", money);
        PlayerPrefs.SetInt("PlayerGem", gem);
        PlayerPrefs.SetInt("PlayerPower", power);

        PlayerPrefs.SetInt("PlayerHp", hp);
        PlayerPrefs.SetInt("PlayerAtk", atk);
        PlayerPrefs.SetInt("PlayerDef", def);
        PlayerPrefs.SetInt("PlayerCrit", crit);
        PlayerPrefs.SetInt("PlayerMiss", miss);
        PlayerPrefs.SetInt("PlayerLuck", luck);

        UpdateInfo();
    }

    public void AddEventListener(UnityAction<MVVM_PlayerModel> function)
    {
        updateEvent += function;
    }

    public void RemoveEventListener(UnityAction<MVVM_PlayerModel> function)
    {
        updateEvent -= function;
    }

    //通知外部更新数据的方法
    private void UpdateInfo()
    {
        //找到对应的 使用数据的脚本 去更新数据
        if( updateEvent != null )
        {
            updateEvent(this);
        }
    }
}

MVVM_MainPanel

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

public class MVVM_MainPanel : BaseUGUIPanel
{
    //1.找控件 通过集成小框架中的 UI基类实现了  
    //2.逻辑处理 
    //3.数据更新
    
    public override void Init()
    {
        //第一次打开时更新
        UpdateInfo(MVVM_PlayerModel.Data);

        MVVM_PlayerModel.Data.AddEventListener(UpdateInfo);
    }

    protected override void OnButtonInitClick(string btnName)
    {
        base.OnButtonInitClick(btnName);
        switch (btnName)
        {
            case "btnRole":
                //处理角色面板打开
                BaseUGUIManager.Instance.ShowPanel<MVVM_RolePanel>(BaseUGUIManager.UI_SAVE_PATH + "MVVM/");
                break;
        }
    }

    public void UpdateInfo(MVVM_PlayerModel data)
    {
        //直接在这获取控件 进行更新
        GetUIElement<Text>("txtName").text = data.PlayerName;
        GetUIElement<Text>("txtLev").text = "LV." + data.Lev;

        GetUIElement<Text>("txtMoney").text = data.Money.ToString();
        GetUIElement<Text>("txtGem").text = data.Gem.ToString();
        GetUIElement<Text>("txtPower").text = data.Power.ToString();
    }

    private void OnDestroy()
    {
        MVVM_PlayerModel.Data.RemoveEventListener(UpdateInfo);
    }
}

MVVMMain

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

public class MVVMMain : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            //显示主面板
            BaseUGUIManager.Instance.ShowPanel<MVVM_MainPanel>(BaseUGUIManager.UI_SAVE_PATH + "MVVM/");
        }
        else if (Input.GetKeyDown(KeyCode.N))
        {
            //隐藏主面板
            BaseUGUIManager.Instance.HidePanel<MVVM_MainPanel>();
        }
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏