18.MVE的实现

18.MVX框架-MVE框架-框架实现


18.1 知识点

相比于MVVM,主要修改是在各个面板初始化时使用事件中心添加事件监听,销毁时移除监听

public class MVE_MainPanel : BaseUGUIPanel
{
    public override void Init()
    {
        //第一次打开时更新
        UpdateInfo(MVE_PlayerModel.Data);

        BaseEventManager.Instance.AddEventListener<MVE_PlayerModel>("MVE_PlayerModel", UpdateInfo);
    }

    ...

    private void OnDestroy()
    {
        BaseEventManager.Instance.RemoveEventListener<MVE_PlayerModel>("MVE_PlayerModel", UpdateInfo);
    }
}

public class MVE_RolePanel : BaseUGUIPanel
{
    public override void Init()
    {
        //第一次显示时 更新面板
        UpdateInfo(MVE_PlayerModel.Data);

        BaseEventManager.Instance.AddEventListener<MVE_PlayerModel>("MVE_PlayerModel", UpdateInfo);
    }

    ...

    private void OnDestroy()
    {
        BaseEventManager.Instance.RemoveEventListener<MVE_PlayerModel>("MVE_PlayerModel", UpdateInfo);
    }
}

数据层更新回调时也使用事件中心

public class MVE_PlayerModel
{
    ...

    // 保存 
    public void SaveData()
    {
        //把这些数据内容 存储到本地
        ...

        UpdateInfo();
    }


    //通知外部更新数据的方法
    private void UpdateInfo()
    {
        BaseEventManager.Instance.EventTrigger<MVE_PlayerModel>("MVE_PlayerModel", this);
    }
}

18.2 知识点代码

MVEMain

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

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

MVE_PlayerModel

using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using BaseFramework;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// 作为一个唯一的数据模型 一般情况 要不自己是个单例模式对象
/// 要不自己存在在一个单例模式对象中
/// </summary>
public class MVE_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 static MVE_PlayerModel data = null;

    public static MVE_PlayerModel Data
    {
        get
        {
            if (data == null)
            {
                data = new MVE_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();
    }


    //通知外部更新数据的方法
    private void UpdateInfo()
    {
        BaseEventManager.Instance.EventTrigger<MVE_PlayerModel>("MVE_PlayerModel", this);
    }
}

MVE_MainPanel

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

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

        BaseEventManager.Instance.AddEventListener<MVE_PlayerModel>("MVE_PlayerModel", UpdateInfo);
    }

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

    public void UpdateInfo(MVE_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()
    {
        BaseEventManager.Instance.RemoveEventListener<MVE_PlayerModel>("MVE_PlayerModel", UpdateInfo);
    }
}

MVE_RolePanel

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

public class MVE_RolePanel : BaseUGUIPanel
{
    //1.找控件
    //2.处理逻辑
    //3.面板更新    

    public override void Init()
    {
        //第一次显示时 更新面板
        UpdateInfo(MVE_PlayerModel.Data);

        BaseEventManager.Instance.AddEventListener<MVE_PlayerModel>("MVE_PlayerModel", UpdateInfo);
    }

    protected override void OnButtonInitClick(string btnName)
    {
        base.OnButtonInitClick(btnName);
        switch (btnName)
        {
            case "btnClose":
                BaseUGUIManager.Instance.HidePanel<MVE_RolePanel>();
                break;
            case "btnLevUp":
                MVE_PlayerModel.Data.LevUp();
                break;
        }
    }

    public void UpdateInfo(MVE_PlayerModel data)
    {
        GetUIElement<Text>("txtLev").text = "LV." + data.Lev;
        GetUIElement<Text>("txtHp").text = data.HP.ToString();
        GetUIElement<Text>("txtAtk").text = data.Atk.ToString();
        GetUIElement<Text>("txtDef").text = data.Def.ToString();
        GetUIElement<Text>("txtCrit").text = data.Crit.ToString();
        GetUIElement<Text>("txtMiss").text = data.Miss.ToString();
        GetUIElement<Text>("txtLuck").text = data.Luck.ToString();
    }

    private void OnDestroy()
    {
        BaseEventManager.Instance.RemoveEventListener<MVE_PlayerModel>("MVE_PlayerModel", UpdateInfo);
    }
}


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

×

喜欢就点赞,疼爱就打赏