M:数据模型//此例两界面共用一个数据模型
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
///
/// 数据模型
///
public class PlayerMode
{
#region//数据层
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 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 miss;
public int Miss
{
get { return miss; }
}
//通过事件与外部联系
public event Action<PlayerMode> updateEvent;
//数据模型类单例,达到数据唯一,与获取数据
private static PlayerMode date;
public static PlayerMode Date
{
get
{
if(date==null)
{
date = new PlayerMode();
date.Init();
}
return date;
}
}
#endregion
//初始化
public void Init()
{
playername = PlayerPrefs.GetString("Playername", "Wei");//PlayerPrefs类场景中存储值
lev = PlayerPrefs.GetInt("Lev", 17);
money = PlayerPrefs.GetInt("Money", 100);
power = PlayerPrefs.GetInt("Power", 100);
hp = PlayerPrefs.GetInt("Hp", 100);
atk = PlayerPrefs.GetInt("Atk", 100);
def = PlayerPrefs.GetInt("Def", 100);
miss = PlayerPrefs.GetInt("Miss", 100);
}
//更新
public void LeveUp()
{
lev += 1;
hp += 10;
atk += 10;
def += 10;
miss += 10;
SaveDate();//更新后保存数据
}
//保存
public void SaveDate()
{
PlayerPrefs.SetString("Playername", playername);
PlayerPrefs.SetInt("Lev", lev);
PlayerPrefs.SetInt("Money", money);
PlayerPrefs.SetInt("Power", power);
PlayerPrefs.SetInt("Hp", hp);
PlayerPrefs.SetInt("Atk", atk);
PlayerPrefs.SetInt("Def", def);
PlayerPrefs.SetInt("Miss", miss);
UpdateInfo();
}
//外部注册事件
public void AddEventLissent(Action<PlayerMode> function)
{
updateEvent += function;
}
//外部取消注册事件
public void RemoveEventLissent(Action<PlayerMode> function)
{
updateEvent -= function;
}
//通知更新数据的方法(调用数据更新时执行的事件)
public void UpdateInfo()
{
updateEvent?.Invoke(this);
}
}
V:界面脚本//获取控件,更新控件数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//VM
public class PlayerMainView : MonoBehaviour
{
//获取控件
public Button btnRole;
public Button btnSkill;
public Text txtName;
public Text txtLev;
public Text txtMoney;
public Text txtPower;
//提供面板更新相关方法给外部
public void UpdateInfo(PlayerMode date)
{
txtName.text = date.Playername;
txtLev.text = "Lv:" + date.Lev.ToString();
txtMoney.text = date.Money.ToString();
txtPower.text = date.Power.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//VR
public class PlayerRoleView : MonoBehaviour
{
//获取控件
public Button btnBack;
public Button btnLeveUp;
public Text txtHp;
public Text txtAtk;
public Text txtDef;
public Text txtMiss;
//提供面板更新相关方法给外部
public void UpdateInfo(PlayerMode date)
{
txtHp.text = date.Hp.ToString();
txtAtk.text = date.Atk.ToString();
txtDef.text = date.Def.ToString();
txtMiss.text = date.Miss.ToString();
}
}
C:界面控制脚本//显隐,初始化,界面逻辑,数据更新时调用的事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 处理主界面业务逻辑
///
public class MainController : MonoBehaviour
{
//能够在Controller中获取View界面
private PlayerMainView mainView;
private static MainController contorller;
public static MainController Controller
{
get
{
return contorller;
}
}
//界面显示隐藏
public static void ShowPanel()
{
if(contorller==null)
{
GameObject res = Resources.Load<GameObject>("UI/MainPanel");
GameObject obj = Instantiate(res);
obj.transform.SetParent(GameObject.Find("Canvas").transform);
contorller = obj.GetComponent<MainController>();
}
}
public static void HeadPanel()
{
contorller.gameObject.SetActive(false);
}
private void Start()
{
//获取挂载同一物体上的View脚本
mainView = this.GetComponent<PlayerMainView>();
//第一次界面更新
mainView.UpdateInfo(PlayerMode.Date);
mainView.btnRole.onClick.AddListener(OnRoleBtnClick);
//数据更新时同时更新界面
PlayerMode.Date.AddEventLissent(UpdateInfo);
}
private void OnRoleBtnClick()
{
RoleController.ShowPanel();
}
//界面事件监听,逻辑处理
//界面更新
private void UpdateInfo(PlayerMode date)
{
if(mainView!=null)
mainView.UpdateInfo(date);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 处理属性界面业务逻辑
///
public class RoleController : MonoBehaviour
{
//能够在Controller中获取View界面
private PlayerRoleView roleView;
private static RoleController contorller;
public static RoleController Controller
{
get
{
return contorller;
}
}
//界面显示隐藏
public static void ShowPanel()
{
if (contorller == null)
{
GameObject res = Resources.Load<GameObject>("UI/RolePanel");
GameObject obj = Instantiate(res);
obj.transform.SetParent(GameObject.Find("Canvas").transform);
contorller = obj.GetComponent<RoleController>();
}
}
public static void HeadPanel()
{
contorller.gameObject.SetActive(false);
}
private void Start()
{
//获取挂载同一物体上的View脚本
roleView = this.GetComponent<PlayerRoleView>();
//第一次界面更新
roleView.UpdateInfo(PlayerMode.Date);
roleView.btnBack.onClick.AddListener(OnClickBack);
roleView.btnLeveUp.onClick.AddListener(OnClickLeveUp);
//数据更新时同时更新界面
PlayerMode.Date.AddEventLissent(UpdataInfo);
//PlayerMode.Date.AddEventLissent(roleView.UpdateInfo);
//PlayerMode.Date.updateEvent += roleView.UpdateInfo;
}
private void OnClickBack()
{
HeadPanel();
}
private void OnClickLeveUp()
{
//通过事件模块,改变数据。
PlayerMode.Date.LeveUp();
}
//界面事件监听,逻辑处理
//界面更新
private void UpdataInfo(PlayerMode date)
{
if(roleView!=null)
roleView.UpdateInfo(date);
}
}