12.MVX框架-MVP框架-Model数据层和View界面层
12.1 知识点
数据层和MVC中的一致,不需要重新写,改个名字就行
主面板界面脚本和角色面板界面脚本可以直接复制MVC中的再进行修改。在主面板界面和角色面板界面中可以删除自身更新面板的逻辑留在逻辑层处理,要保留的话也不要和数据层耦合,直接传入参数
// MVC是这样的 会和PlayerModel关联
// public void UpdateInfo( PlayerModel data )
// {
// txtName.text = data.PlayerName;
// txtLev.text = "LV." + data.Lev;
// txtMoney.text = data.Money.ToString();
// txtGem.text = data.Gem.ToString();
// txtPower.text = data.Power.ToString();
// }
//MVP中以下方法可选 也可以在P中写 但是不可以和PlayerModel
//public void UpdateInfo( string name, int lev, int money, int gem, int power )
//{
// txtName.text = name;
// txtLev.text = lev.ToString();
// txtMoney.text = money.ToString();
// txtGem.text = gem.ToString();
// txtPower.text = power.ToString();
//}
12.2 知识点代码
MVP_MainView
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MVP_MainView : MonoBehaviour
{
//1.找控件
public Button btnRole;
public Button btnSill;
public Text txtName;
public Text txtLev;
public Text txtMoney;
public Text txtGem;
public Text txtPower;
////2.更新数据
//public void UpdateInfo( string name, int lev, int money, int gem, int power )
//{
// txtName.text = name;
// txtLev.text = lev.ToString();
// txtMoney.text = money.ToString();
// txtGem.text = gem.ToString();
// txtPower.text = power.ToString();
//}
}
MVP_RoleView
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MVP_RoleView : MonoBehaviour
{
//1.找控件
public Button btnClose;
public Button btnLevUp;
public Text txtLev;
public Text txtHp;
public Text txtAtk;
public Text txtDef;
public Text txtCrit;
public Text txtMiss;
public Text txtLuck;
//2.提供面板更新的相关方法给外部
//方法可选 你到时候可以直接在P里面通过访问控件 去修改
}
MVP_PlayerModel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// 作为一个唯一的数据模型 一般情况 要不自己是个单例模式对象
/// 要不自己存在在一个单例模式对象中
/// </summary>
public class MVP_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<MVP_PlayerModel> updateEvent;
//在外部第一次获取这个数据 如何获取
//通过单例模式 来达到数据的唯一性 和数据的获取
private static MVP_PlayerModel data = null;
public static MVP_PlayerModel Data
{
get
{
if( data == null )
{
data = new MVP_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<MVP_PlayerModel> function)
{
updateEvent += function;
}
public void RemoveEventListener(UnityAction<MVP_PlayerModel> function)
{
updateEvent -= function;
}
//通知外部更新数据的方法
private void UpdateInfo()
{
//找到对应的 使用数据的脚本 去更新数据
if( updateEvent != null )
{
updateEvent(this);
}
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com