您好,登錄后才能下訂單哦!
本篇簡單介紹一下,Unity中MVC框架的簡單應用。MVC自1982年被設計出來,至今都有著很大比重的使用率,特別是目前軟件及游戲迭代此之快的情況下。高效的增刪功能低耦合又小巧靈活的框架MVC,深受廣大ProgramDesigner的喜愛。
數據是程序的靈魂,視圖是看清靈魂的眼睛,控制器從中撥開迷霧,這就形成了現在的MVC。
簡單實例:Model
using UnityEngine;
using System.Collections;
/// <summary>
/// 模型委托(當用戶信息發生變化時執行)
/// </summary>
public delegate void OnValueChange (int val);
public class PlayerMsgModel
{
//玩家等級
private int playerLevel;
//玩家經驗
private int playerExperience;
//玩家升級經驗
private int playerFullExperience;
//金幣數量
private int goldNum;
//聲明委托對象,接收當等級發生變化時,觸發的事件
public OnValueChange OnLevelChange;
//聲明委托對象,接收當經驗發生變化時,觸發的事件
public OnValueChange OnExperienceChange;
//聲明委托對象,接收當升級經驗發生變化時,觸發的事件
public OnValueChange OnFullExperienceChange;
//聲明委托對象,接收當金幣數量發生變化時,觸發的事件
public OnValueChange OnGoldNumChange;
//單例
private static PlayerMsgModel mod;
public static PlayerMsgModel GetMod ()
{
if (mod == null) {
mod = new PlayerMsgModel ();
}
return mod;
}
private PlayerMsgModel ()
{
}
/// <summary>
/// 玩家等級屬性
/// </summary>
/// <value>The player level.</value>
public int PlayerLevel {
get {
return playerLevel;
}
set {
playerLevel = value;
//如果委托對象不為空
if (OnLevelChange != null) {
//執行委托
OnLevelChange (playerLevel);
}
}
}
/// <summary>
/// 玩家經驗屬性
/// </summary>
/// <value>The player experience.</value>
public int PlayerExperience {
get {
return playerExperience;
}
set {
playerExperience = value;
if (OnExperienceChange != null) {
OnExperienceChange (playerExperience);
}
}
}
/// <summary>
/// 玩家升級經驗屬性
/// </summary>
/// <value>The player full experience.</value>
public int PlayerFullExperience {
get {
return playerFullExperience;
}
set {
playerFullExperience = value;
if (OnFullExperienceChange != null) {
OnFullExperienceChange (playerFullExperience);
}
}
}
/// <summary>
/// 金幣數量屬性
/// </summary>
/// <value>The gold number.</value>
public int GoldNum {
get {
return goldNum;
}
set {
goldNum = value;
if (OnGoldNumChange != null) {
OnGoldNumChange (goldNum);
}
}
}
}
簡單實例:View
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerMsgView : MonoBehaviour
{
//UI
public Text playerLevel;
public Text playerExperience;
public Text goldNum;
public Button experienceUpButton;
void Start ()
{
//委托事件綁定
PlayerMsgModel.GetMod ().OnLevelChange += SetLevel;
//委托事件綁定
PlayerMsgModel.GetMod ().OnExperienceChange += SetExperience;
PlayerMsgModel.GetMod ().OnFullExperienceChange += SetFullExperience;
PlayerMsgModel.GetMod ().OnGoldNumChange += SetGoldNum;
//View綁定按鈕控制功能
experienceUpButton.onClick.AddListener (
PlayerMsgController.controller.OnExperienceUpButtonClick);
}
//修改UILevel值
public void SetLevel (int level)
{
playerLevel.text = level.ToString ();
}
//修改UI經驗值
public void SetExperience (int experience)
{
//將字符串以“/”拆開
string[] str = playerExperience.text.Split (new char []{ '/' });
//用新的經驗值重組
playerExperience.text = experience + "/" + str [1];
}
public void SetFullExperience (int fullExiperience)
{
string[] str = playerExperience.text.Split (new char []{ '/' });
playerExperience.text = str [0] + "/" + fullExiperience;
}
public void SetGoldNum (int goldn)
{
goldNum.text = goldn.ToString ();
}
}
簡單實例:Controller
using UnityEngine;
using System.Collections;
public class PlayerMsgController : MonoBehaviour
{
public static PlayerMsgController controller;
private int levelUpValue = 20;
void Awake ()
{
controller = this;
}
void Start ()
{
PlayerMsgModel.GetMod ().PlayerLevel = 1;
PlayerMsgModel.GetMod ().PlayerExperience = 0;
PlayerMsgModel.GetMod ().PlayerFullExperience = 100;
PlayerMsgModel.GetMod ().GoldNum = 0;
}
/// <summary>
/// 提升經驗按鈕點擊事件
/// </summary>
public void OnExperienceUpButtonClick ()
{
PlayerMsgModel.GetMod ().PlayerExperience += levelUpValue;
if (PlayerMsgModel.GetMod ().PlayerExperience
>= PlayerMsgModel.GetMod ().PlayerFullExperience) {
PlayerMsgModel.GetMod ().PlayerLevel += 1;
PlayerMsgModel.GetMod ().PlayerFullExperience +=
200 * PlayerMsgModel.GetMod ().PlayerLevel;
levelUpValue += 20;
if (PlayerMsgModel.GetMod ().PlayerLevel % 3 == 0) {
PlayerMsgModel.GetMod ().GoldNum +=
100 * PlayerMsgModel.GetMod ().PlayerLevel;
}
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。