目录
Unity 工具类 之 简单的 MVC 框架使用实现简单的案例demo
一、简单介绍
二、实现原理
三、注意事项
四、效果预览
五、实现步骤
六、关键代码
七、参考工程
Unity 工具类,自己整理的一些游戏开发可能用到的模块,单独独立使用,方便游戏开发。
Unity中MVC框架的简单应用。MVC自1982年被设计出来,至今都有着很大比重的使用率,特别是目前软件及游戏迭代此之快的情况下。高效的增删功能低耦合又小巧灵活的框架MVC,深受广大ProgramDesigner的喜爱。
1、单例类,保证整个场景中只有一个类管理相关类;
2、MVC 每个 每个独立成快,又有简单的联系;
3、MVC 框架简单如下图;
4、换种形式的 MVC 业务流程
5、MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面 显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于 映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中
1、这里是一个简单的MVC思想的使用,具体使用形式根据自己的情况,酌情做出改变;
1、打开Unity,新建一个工程,在场景中简单布局一些 UI,如下图
2、在工程中,新建一写脚本,单例类使类唯一,Model 类管理数据,View 类 管理 UI 视图,Controller 类一些逻辑控制,如下图
3、把 View脚本挂载到 canvas 上,并赋值对应 UI,如下图
4、把 Controller 脚本挂载到场景中,如下图
5、运行场景,点击按钮,增加经验,经验、等级、金币变化效果如下
1、PlayerMsgModel
using UnityEngine;
using System.Collections;
///
/// 模型委托(当用户信息发生变化时执行)
///
public delegate void OnValueChange(int val);
public class PlayerMsgModel : Singleton
{
//玩家等级
private int playerLevel;
//玩家经验
private int playerExperience;
//玩家升级经验
private int playerFullExperience;
//金币数量
private int goldNum;
//声明委托对象,接收当等级发生变化时,触发的事件
public OnValueChange OnLevelChange;
//声明委托对象,接收当经验发生变化时,触发的事件
public OnValueChange OnExperienceChange;
//声明委托对象,接收当升级经验发生变化时,触发的事件
public OnValueChange OnFullExperienceChange;
//声明委托对象,接收当金币数量发生变化时,触发的事件
public OnValueChange OnGoldNumChange;
///
/// 玩家等级属性
///
/// The player level.
public int PlayerLevel
{
get
{
return playerLevel;
}
set
{
playerLevel = value;
//如果委托对象不为空
if (OnLevelChange != null)
{
//执行委托
OnLevelChange(playerLevel);
}
}
}
///
/// 玩家经验属性
///
/// The player experience.
public int PlayerExperience
{
get
{
return playerExperience;
}
set
{
playerExperience = value;
if (OnExperienceChange != null)
{
OnExperienceChange(playerExperience);
}
}
}
///
/// 玩家升级经验属性
///
/// The player full experience.
public int PlayerFullExperience
{
get
{
return playerFullExperience;
}
set
{
playerFullExperience = value;
if (OnFullExperienceChange != null)
{
OnFullExperienceChange(playerFullExperience);
}
}
}
///
/// 金币数量属性
///
/// The gold number.
public int GoldNum
{
get
{
return goldNum;
}
set
{
goldNum = value;
if (OnGoldNumChange != null)
{
OnGoldNumChange(goldNum);
}
}
}
}
2、PlayerMsgView
using UnityEngine;
using UnityEngine.UI;
public class PlayerMsgView : MonoBehaviour
{
//UI
public Text playerLevel;
public Text playerExperience;
public Text goldNum;
public Button experienceUpButton;
void Start()
{
InitView();
}
///
///初始化 VIew 相关
///
void InitView() {
//委托事件绑定
PlayerMsgModel.Instance.OnLevelChange += SetLevel;
PlayerMsgModel.Instance.OnExperienceChange += SetExperience;
PlayerMsgModel.Instance.OnFullExperienceChange += SetFullExperience;
PlayerMsgModel.Instance.OnGoldNumChange += SetGoldNum;
//View绑定按钮控制功能
experienceUpButton.onClick.AddListener(
PlayerMsgController.Instance.OnExperienceUpButtonClick);
}
//修改UILevel值
public void SetLevel(int level)
{
playerLevel.text = "Level: "+level.ToString();
}
//修改UI经验值( 格式:exp: 50 / 100)
public void SetExperience(int experience)
{
string[] str = playerExperience.text.Split(new char[] { '/' });
//用新的经验值重组
playerExperience.text = "Exp: "+experience +"/"+ str[1];
}
//修改UI经验值( 格式:exp: 50 / 100)
public void SetFullExperience(int fullExiperience)
{
string[] str = playerExperience.text.Split(new char[] { '/' });
playerExperience.text = str[0] + "/" + fullExiperience;
}
public void SetGoldNum(int goldn)
{
goldNum.text = "Gold: "+goldn.ToString();
}
}
3、PlayerMsgController
using UnityEngine;
using System.Collections;
public class PlayerMsgController : MonoSingleton
{
// 升级的初始经验增加值
private int levelUpValue = 20;
void Start()
{
InitModel();
}
///
/// 初始化数据
///
void InitModel()
{
PlayerMsgModel.Instance.PlayerLevel = 1;
PlayerMsgModel.Instance.PlayerExperience = 0;
PlayerMsgModel.Instance.PlayerFullExperience = 100;
PlayerMsgModel.Instance.GoldNum = 0;
}
///
/// 提升经验按钮点击事件
///
public void OnExperienceUpButtonClick()
{
PlayerMsgModel.Instance.PlayerExperience += levelUpValue;
if (PlayerMsgModel.Instance.PlayerExperience
>= PlayerMsgModel.Instance.PlayerFullExperience)
{
PlayerMsgModel.Instance.PlayerLevel += 1;
PlayerMsgModel.Instance.PlayerFullExperience +=
200 * PlayerMsgModel.Instance.PlayerLevel;
levelUpValue += 20;
if (PlayerMsgModel.Instance.PlayerLevel % 3 == 0)
{
PlayerMsgModel.Instance.GoldNum +=
100 * PlayerMsgModel.Instance.PlayerLevel;
}
}
}
}
4、Singleton
public abstract class Singleton where T : class, new()
{
private static T instance = null;
// 多线程安全机制
private static readonly object locker = new object();
public static T Instance
{
get
{
lock (locker)
{
if (instance == null)
instance = new T();
return instance;
}
}
}
}
5、MonoSingleton
using UnityEngine;
public abstract class MonoSingleton : MonoBehaviour where T : MonoBehaviour
{
private static T instance = null;
private static readonly object locker = new object();
private static bool bAppQuitting;
public static T Instance
{
get
{
if (bAppQuitting)
{
instance = null;
return instance;
}
lock (locker)
{
if (instance == null)
{
instance = FindObjectOfType();
if (FindObjectsOfType().Length > 1)
{
Debug.LogError("不应该存在多个单例!");
return instance;
}
if (instance == null)
{
var singleton = new GameObject();
instance = singleton.AddComponent();
singleton.name = "(singleton)" + typeof(T);
singleton.hideFlags = HideFlags.None;
DontDestroyOnLoad(singleton);
}
else
DontDestroyOnLoad(instance.gameObject);
}
instance.hideFlags = HideFlags.None;
return instance;
}
}
}
private void Awake()
{
bAppQuitting = false;
}
private void OnDestroy()
{
bAppQuitting = true;
}
}
点击下载工程