
前言:
1.關(guān)于PureMVC:
MVC框架在很多項(xiàng)目當(dāng)中擁有廣泛的應(yīng)用,很多時(shí)候做項(xiàng)目前人開(kāi)坑開(kāi)了一半就消失了,后人為了填補(bǔ)各種的坑就遭殃的不得了。嘛,程序猿大家都不喜歡像文案策劃一樣組織文字寫(xiě)東西,也不喜歡看別人留下不清不楚的文檔,還不如看代碼來(lái)得實(shí)在。剛開(kāi)始新人看代碼是看得頭暈,因?yàn)檫@樣那樣的東西不一定能完全猜得透。而老人家就算有經(jīng)驗(yàn)和閱歷,也沒(méi)有這么多的體力去看一個(gè)龐大而又復(fù)雜的糟糕項(xiàng)目。因?yàn)檫@種需求,Unity3D的程序猿就統(tǒng)一組織起來(lái),想做一個(gè)完整規(guī)范的程序框架,而這時(shí),PureMVC就誕生了。我個(gè)人喜歡PureMVC的原因也很簡(jiǎn)單,因?yàn)樗?jiǎn)單粗暴,和Unity3D之間沒(méi)有依賴性,加上又開(kāi)源,真的遇到Bug能拿到源代碼來(lái)調(diào)試也是很容易執(zhí)行的。Unity3D應(yīng)用商店還有一個(gè)同類產(chǎn)品叫uFrame Game Framework,它對(duì)Unity版本有著依賴,拖視圖雖然方便,但是一旦出bug真的改得夠嗆的,所以不推薦使用它。下文便是使用PureMVC和Unity3D的UGUI制作一個(gè)簡(jiǎn)單的員工管理系統(tǒng)實(shí)例。
2.通過(guò)MVC模式在Unity項(xiàng)目當(dāng)中應(yīng)用的特別提醒:
(1)Unity3D是基于組件設(shè)計(jì)的,如果沒(méi)有好的規(guī)劃,組件之間會(huì)產(chǎn)生復(fù)雜的調(diào)用關(guān)系,導(dǎo)致組件之間復(fù)雜的依賴,從而破壞了整個(gè)系統(tǒng)結(jié)構(gòu),因此需要設(shè)計(jì)時(shí)確定組件的層次關(guān)系,確保依賴關(guān)系只存在于下層對(duì)上層。而這個(gè)是業(yè)務(wù)邏輯設(shè)計(jì)問(wèn)題,PureMVC幫不了你。
(2)僅僅用上MVC,解決不了什么問(wèn)題,或許解決了1%,剩下的99%就被挪到了MVC的C里,當(dāng)你慶祝MVC竣工時(shí),99%的問(wèn)題在那里默默的微笑的看著你。(話說(shuō)以前寫(xiě)PHP的CI框架時(shí)候,一堆東西扔到XxxAction.Class.php里面,發(fā)現(xiàn)和擺的亂七八糟的架構(gòu)沒(méi)區(qū)別,只是大家都習(xí)慣了這套框架的規(guī)矩,看代碼找某個(gè)東西稍微好找而已,本質(zhì)上還是考驗(yàn)基本功和對(duì)項(xiàng)目的熟悉程度的,23333)
3.PureMVC通過(guò)4種pattern實(shí)現(xiàn)隔離變化:
(1)facade非常適合將UI界面對(duì)游戲數(shù)據(jù)的依賴解耦,將UI操作數(shù)據(jù)的請(qǐng)求封裝在facade 接口里面,同時(shí)數(shù)據(jù)變化對(duì)UI的影響可以通過(guò)notification事件通知來(lái)實(shí)現(xiàn),該模式應(yīng)用得非常常見(jiàn)。
(2)command模式統(tǒng)一對(duì)對(duì)象的操作,將鍵盤(pán)輸入,網(wǎng)絡(luò)輸入輸出統(tǒng)一成command來(lái)操控游戲?qū)ο蟆?br>
(3)proxy維護(hù)數(shù)據(jù),提供某塊數(shù)據(jù)統(tǒng)一的初始化,訪問(wèn)和修改接口。
(4)mediator沒(méi)怎么用過(guò),我們的游戲中的UI界面每次變化一般都是整體更新的,不常用局部更新。
以上4中pattern請(qǐng)務(wù)必牢牢記住,請(qǐng)務(wù)必牢牢記住,請(qǐng)務(wù)必牢牢記住。重要的事情要說(shuō)3便。
4.PureMVC的流程示意圖
(1)在puremvc中,model/view/controller統(tǒng)一是由Facade類的單件實(shí)例來(lái)統(tǒng)籌管理的。
(2)PureMVC的基本流程:?jiǎn)?dòng)PureMVC—>建立Mediator來(lái)操作視覺(jué)元素(按鈕與文本框)—>點(diǎn)擊按鈕發(fā)送Notification->文本框接收Notification改變內(nèi)容。
(3)大致流程可理解為:通過(guò)Facade類的單件實(shí)例(即:統(tǒng)一的門(mén)面) 啟動(dòng) puremvc環(huán)境,啟動(dòng)同時(shí)注冊(cè)Command對(duì)象(相當(dāng)于asp.net mvc中的controller),然后Command通過(guò)與之關(guān)聯(lián)的facade(即前面的單件實(shí)例)來(lái)注冊(cè)Mediator(中介者:用于把View與Command聯(lián)系起來(lái))。
(4)當(dāng)UI界面(即View)上有動(dòng)靜時(shí)(比如按鈕點(diǎn)擊了之類),與之關(guān)聯(lián)的Mediator(中介者)會(huì)發(fā)送通知給facade,然后facade會(huì)調(diào)用command對(duì)象執(zhí)行相關(guān)的處理。(即:消息響應(yīng))

一.引入PureMVC的插件
1.下載PureMVC
請(qǐng)?jiān)L問(wèn)地址
https://github.com/PureMVC/puremvc-csharp-standard-framework/wiki
安裝
2.把PureMVC.DotNET.35.dll放到Plugins里面就好了。
QA
3.這里有一個(gè)很簡(jiǎn)單的基本案例可以參考一下
http://www.open-open.com/lib/view/open1452657515480.html
二.動(dòng)手配置文件
1.需要完成的實(shí)例如下:

2.具體實(shí)現(xiàn)的目標(biāo):
(1)在Scripts文件夾下,分別設(shè)置模型、視圖、控制器對(duì)應(yīng)的文件夾Model、View、Controller,分別放置處理數(shù)據(jù)模型的腳本、處理顯示視圖的腳本、處理邏輯控制的腳本。
(2)如界面,一個(gè)Unity3D和UGUI制作的簡(jiǎn)單員工管理系統(tǒng),Employee Admin,其中員工界面Users顯示有哪些員工在登記范圍內(nèi),而New和Delete分別是添加和刪除某個(gè)員工的信息。然后下面的員工信息界面User Profile則是對(duì)員工信息的一個(gè)具體編輯和修正。
三.主要實(shí)現(xiàn)步驟
1.啟動(dòng)文件AppFacade.cs 作為PureMVC框架的入口文件。
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;
//Facade模式的單例
public class ApplicationFacade : Facade
{
//實(shí)例化函數(shù),保證單例模式(Singleton)運(yùn)行該函數(shù)
public new static IFacade Instance
{
get
{
if(m_instance == null)
{
lock(m_staticSyncRoot)
{
if (m_instance == null)
{
Debug.Log("ApplicationFacade");
m_instance = new ApplicationFacade();
}
}
}
return m_instance;
}
}
//啟動(dòng)PureMVC的入口函數(shù)
public void Startup(MainUI mainUI)
{
Debug.Log("Startup() to SendNotification.");
SendNotification(EventsEnum.STARTUP, mainUI);
}
//該類的構(gòu)造器
protected ApplicationFacade()
{
}
//設(shè)置靜態(tài)
static ApplicationFacade()
{
}
//初始化控制器函數(shù)
protected override void InitializeController()
{
Debug.Log("InitializeController()");
base.InitializeController();
RegisterCommand(EventsEnum.STARTUP, typeof(StartupCommand));
RegisterCommand(EventsEnum.DELETE_USER, typeof(DeleteUserCommand));
}
}
2.對(duì)PureMVC需要處理的事件用EventsEnum.cs存放
using UnityEngine;
using System.Collections;
//處理事件的枚舉
public class EventsEnum
{
public const string STARTUP = "startup";//啟動(dòng)事件
public const string NEW_USER = "newUser";//新建用戶
public const string DELETE_USER = "deleteUser";//刪除用戶
public const string CANCEL_SELECTED = "cancelSelected";//取消選擇
public const string USER_SELECTED = "userSelected";//選擇用戶
public const string USER_ADDED = "userAdded";//添加用戶
public const string USER_UPDATED = "userUpdated";//更新用戶
public const string USER_DELETED = "userDeleted";//刪除用戶
public const string ADD_ROLE = "addRole";//添加角色
public const string ADD_ROLE_RESULT = "addRoleResult";//查詢添加角色的結(jié)果
}
3.然后在Unity的場(chǎng)景中創(chuàng)建一個(gè)MainUI.cs文件,掛在需要啟動(dòng)PureMVC的組件上。就可以啟動(dòng)了。
using UnityEngine;
using System.Collections;
//處理該UI場(chǎng)景的入口
public class MainUI : MonoBehaviour
{
public UserList userList;
public UserForm userForm;
//啟動(dòng)函數(shù)
void Awake()
{
//啟動(dòng)PureMVC程序,執(zhí)行StartUP()方法
ApplicationFacade facade = ApplicationFacade.Instance as ApplicationFacade;
facade.Startup(this);
}
}
4.對(duì)Controller部分進(jìn)行處理
然后我們對(duì)執(zhí)行邏輯的處理事件進(jìn)行補(bǔ)充。新建一個(gè)文件夾Controller,暫時(shí)先放置StartupCommand.cs和DeleteUserCommand.cs。處理上述所說(shuō)的邏輯事件
首先,處理啟動(dòng)事件
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;
//啟動(dòng)事件
public class StartupCommand : SimpleCommand, ICommand
{
//復(fù)寫(xiě)原有的Execute執(zhí)行函數(shù)
public override void Execute(INotification notification)
{
//注冊(cè)統(tǒng)一的數(shù)據(jù)接口UserProxy,給其他事件處理
Debug.Log("StartupCommand.Execute()");
Facade.RegisterProxy(new UserProxy());
//注冊(cè)局部界面Mediator,給其他事件處理
MainUI mainUI = notification.Body as MainUI;
Facade.RegisterMediator(new UserListMediator(mainUI.userList));
Facade.RegisterMediator(new UserFormMediator(mainUI.userForm));
}
}
其次,處理刪除用戶事件
using PureMVC.Patterns;
using PureMVC.Interfaces;
//刪除用戶事件
public class DeleteUserCommand : SimpleCommand, ICommand
{
//復(fù)寫(xiě)原有的Execute執(zhí)行函數(shù)
public override void Execute(INotification notification)
{
//獲取要?jiǎng)h除的對(duì)象user
UserVO user = (UserVO)notification.Body;
//獲取處理數(shù)據(jù)操作的userProxy
UserProxy userProxy = (UserProxy)Facade.RetrieveProxy(UserProxy.NAME);
//操作數(shù)據(jù),刪除user
userProxy.DeleteItem(user);
//刪除完畢,廣播USER_DELETED進(jìn)行通知
SendNotification(EventsEnum.USER_DELETED);
}
}
5.對(duì)Model部分進(jìn)行處理
然后是對(duì)Model模型數(shù)據(jù)的定義哈,首先要記錄的信息用UserVO來(lái)表示
using UnityEngine;
using System.Collections;
//顯示用的數(shù)據(jù)信息UserViewObject
public class UserVO
{
//用戶名
public string UserName
{
get { return m_userName; }
}
private string m_userName = "";
//名字
public string FirstName
{
get { return m_firstName; }
}
private string m_firstName = "";
//姓氏
public string LastName
{
get { return m_lastName; }
}
private string m_lastName = "";
//郵箱
public string Email
{
get { return m_email; }
}
private string m_email = "";
//密碼
public string Password
{
get { return m_password; }
}
private string m_password = "";
//部門(mén)
public string Department
{
get { return m_department; }
}
private string m_department = "";
//是否合法
public bool IsValid
{
get
{
return !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password);
}
}
//合并名字
public string GivenName
{
get { return LastName + ", " + FirstName; }
}
//構(gòu)造器
public UserVO()
{
}
//構(gòu)造函數(shù)
public UserVO(string uname, string fname, string lname, string email, string password, string department)
{
if (uname != null) m_userName = uname;
if (fname != null) m_firstName = fname;
if (lname != null) m_lastName = lname;
if (email != null) m_email = email;
if (password != null) m_password = password;
if (department != null) m_department = department;
}
}
接著對(duì)操作數(shù)據(jù)的UserProxy進(jìn)行補(bǔ)充
using UnityEngine;
using System.Collections.Generic;
using PureMVC.Patterns;
using PureMVC.Interfaces;
//數(shù)據(jù)操作
public class UserProxy : Proxy, IProxy
{
public new const string NAME = "UserProxy";
//返回?cái)?shù)據(jù)操作類
public IList<UserVO> Users
{
get { return (IList<UserVO>) base.Data; }
}
//構(gòu)造函數(shù),添加幾條數(shù)據(jù)進(jìn)去先
public UserProxy():base(NAME, new List<UserVO>())
{
Debug.Log("UserProxy()");
//添加幾條測(cè)試用的數(shù)據(jù)
AddItem(new UserVO("lstooge", "Larry", "Stooge", "larry@stooges.com", "ijk456", "ACCT"));
AddItem(new UserVO("cstooge", "Curly", "Stooge", "curly@stooges.com", "xyz987", "SALES"));
AddItem(new UserVO("mstooge", "Moe", "Stooge", "moe@stooges.com", "abc123", "PLANT"));
AddItem(new UserVO("lzh", "abc", "def", "lzh@stooges.com", "abc123", "IT"));
}
//添加數(shù)據(jù)的方法
public void AddItem(UserVO user)
{
Users.Add(user);
}
//更新數(shù)據(jù)的方法
public void UpdateItem(UserVO user)
{
for (int i = 0; i < Users.Count; i++)
{
if (Users[i].UserName.Equals(user.UserName))
{
Users[i] = user;
break;
}
}
}
//刪除數(shù)據(jù)的方法
public void DeleteItem(UserVO user)
{
for (int i = 0; i < Users.Count; i++)
{
if (Users[i].UserName.Equals(user.UserName))
{
Users.RemoveAt(i);
break;
}
}
}
}
6.接下來(lái)對(duì)VIew部分進(jìn)行實(shí)現(xiàn)
顯示用戶界面列表的UserList和填寫(xiě)用戶具體信息的UserForm
列表單條數(shù)據(jù)部分
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
//UserList列表當(dāng)中單條數(shù)據(jù)信息
public class UserList_Item : MonoBehaviour
{
//定義UI組件
public Text txt_userName;//用戶名文本框
public Text txt_firstName;//名字文本框
public Text txt_lastName;//姓氏文本框
public Text txt_email;//郵件文本框
public Text txt_department;//部門(mén)文本框
//定義User信息類
public UserVO userData;
//更新User類
public void UpdateData(UserVO data)
{
//獲取需要更改的User信息
this.userData = data;
//更改UI的文字信息
txt_userName.text = data.UserName;
txt_firstName.text = data.FirstName;
txt_lastName.text = data.LastName;
txt_email.text = data.Email;
txt_department.text = data.Department;
}
}
用戶列表部分
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
//UserList用戶界面列表部分
public class UserList : MonoBehaviour
{
//定義UI組件
public Text txt_userCount;//用戶列表文本框
public UGUI_MyToggleGroup myToggleGroup;//用戶列表滑動(dòng)條
public Button btn_New;//新建按鈕
public Button btn_Delete;//刪除按鈕
public UserList_Item itemPrefab;//單個(gè)列表文件的預(yù)置體
List<UserList_Item> itemList = new List<UserList_Item>();//臨時(shí)存列表
//定義事件操作
public System.Action NewUser;//添加用戶事件
public System.Action DeleteUser;//刪除用戶事件
public System.Action SelectUser;//選擇用戶事件
//定義數(shù)據(jù)
public UserVO SelectedUserData;//列表中選擇好的用戶
private IList<UserVO> m_currentUsers;//當(dāng)前選擇的用戶
//開(kāi)始函數(shù)
void Start ()
{
itemPrefab.gameObject.SetActive(false);
myToggleGroup.onToggleChange.AddListener(onSelectUserItem);
btn_New.onClick.AddListener(onClick_btn_New);
btn_Delete.onClick.AddListener(onClick_btn_Delete);
UpdateButtons();
}
//加載用戶
public void LoadUsers(IList<UserVO> list)
{
m_currentUsers = list;
RefreshUI(list);
}
//點(diǎn)擊新建
void onClick_btn_New()
{
if (NewUser != null) NewUser();
}
//點(diǎn)擊刪除
void onClick_btn_Delete()
{
if (DeleteUser != null) DeleteUser();
}
//選擇物體
void onSelectUserItem(Toggle itemToggle)
{
if (itemToggle == null)
{
return;
}
UserList_Item item = itemToggle.GetComponent<UserList_Item>();
this.SelectedUserData = item.userData;
UpdateButtons();
if (SelectUser != null) SelectUser();
}
//取消選擇
public void Deselect()
{
myToggleGroup.toggleGroup.SetAllTogglesOff();
this.SelectedUserData = null;
UpdateButtons();
}
//刷新UI
void RefreshUI(IList<UserVO> datas)
{
ClearItems();
foreach (var data in datas)
{
UserList_Item item = CreateItem();
item.UpdateData(data);
itemList.Add(item);
}
txt_userCount.text = datas.Count.ToString();
}
//新建列表項(xiàng)目
UserList_Item CreateItem()
{
UserList_Item item = GameObject.Instantiate<UserList_Item>(itemPrefab);
item.transform.SetParent(itemPrefab.transform.parent);
item.gameObject.SetActive(true);
item.transform.localScale = Vector3.one;
item.transform.localPosition = Vector3.zero;
return item;
}
//清空列表
void ClearItems()
{
foreach(var item in itemList)
{
Destroy(item.gameObject);
}
itemList.Clear();
}
//更新按鈕
private void UpdateButtons()
{
btn_Delete.interactable = (SelectedUserData != null);
}
}
用戶個(gè)人信息部分
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
//用戶個(gè)人信息表單模式?編輯:新增
public enum UserFormMode
{
ADD,
EDIT,
}
//用戶個(gè)人信息表單
public class UserForm : MonoBehaviour
{
//UI項(xiàng)的定義
public InputField txt_firstName;
public InputField txt_lastName;
public InputField txt_email;
public InputField txt_userName;
public InputField txt_password;
public InputField txt_confirmPassword;
public InputField txt_department;
public Button btn_updateUser;
public Button btn_cancel;
//其他
public System.Action AddUser;
public System.Action UpdateUser;
public System.Action CancelUser;
//用戶信息獲取
public UserVO User
{
get { return m_user; }
}
private UserVO m_user;
//用戶信息表單
public UserFormMode Mode
{
get { return m_mode; }
}
private UserFormMode m_mode = UserFormMode.ADD;
//開(kāi)始
void Start ()
{
//設(shè)置UI
btn_updateUser.onClick.AddListener(btn_updateUser_Click);
btn_cancel.onClick.AddListener(btn_cancel_Click);
txt_userName.onValueChange.AddListener(InputField_onValueChange);
txt_password.onValueChange.AddListener(InputField_onValueChange);
txt_confirmPassword.onValueChange.AddListener(InputField_onValueChange);
UpdateButtons();
}
//顯示當(dāng)前用戶信息
public void ShowUser(UserVO user, UserFormMode mode)
{
m_mode = mode;
if (user == null)
{
ClearForm();
}
else
{
m_user = user;
txt_firstName.text = user.FirstName;
txt_lastName.text = user.LastName;
txt_email.text = user.Email;
txt_userName.text = user.UserName;
txt_password.text = txt_confirmPassword.text = user != null ? user.Password : "";
txt_department.text = user.Department;
//txt_firstName.ActivateInputField();
EventSystem.current.SetSelectedGameObject(txt_firstName.gameObject);
//txt_firstName.MoveTextEnd(false);
txt_firstName.caretPosition = txt_firstName.text.Length - 1;
UpdateButtons();
}
}
//更新按鈕
private void UpdateButtons()
{
if (btn_updateUser != null)
{
btn_updateUser.interactable = (txt_firstName.text.Length > 0 && txt_password.text.Length > 0 && txt_password.text.Equals(txt_confirmPassword.text));
}
}
//清空表單
public void ClearForm()
{
m_user = null;
txt_firstName.text = txt_lastName.text = txt_email.text = txt_userName.text = "";
txt_password.text = txt_confirmPassword.text = "";
txt_department.text = "";
UpdateButtons();
}
//更新用戶信息
void btn_updateUser_Click()
{
m_user = new UserVO(
txt_userName.text, txt_firstName.text,
txt_lastName.text, txt_email.text,
txt_password.text, txt_department.text);
if (m_user.IsValid)
{
if (m_mode == UserFormMode.ADD)
{
if (AddUser != null) AddUser();
}
else
{
if (UpdateUser != null) UpdateUser();
}
}
}
//取消信息
void btn_cancel_Click()
{
if (CancelUser != null) CancelUser();
}
//輸入
void InputField_onValueChange(string value)
{
UpdateButtons();
}
}
最后,對(duì)局部更新UI的行為進(jìn)行完善Mediator
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;
using System.Collections.Generic;
//更新UserList部分
public class UserListMediator : Mediator, IMediator
{
private UserProxy userProxy;
public new const string NAME = "UserListMediator";
private UserList View
{
get { return (UserList)ViewComponent; }
}
public UserListMediator(UserList userList)
: base(NAME, userList)
{
Debug.Log("UserListMediator()");
userList.NewUser += userList_NewUser;
userList.DeleteUser += userList_DeleteUser;
userList.SelectUser += userList_SelectUser;
}
public override void OnRegister()
{
Debug.Log("UserListMediator.OnRegister()");
base.OnRegister();
userProxy = Facade.RetrieveProxy(UserProxy.NAME) as UserProxy;
View.LoadUsers(userProxy.Users);
}
void userList_NewUser()
{
UserVO user = new UserVO();
SendNotification(EventsEnum.NEW_USER, user);
}
void userList_DeleteUser()
{
SendNotification(EventsEnum.DELETE_USER, View.SelectedUserData);
}
void userList_SelectUser()
{
SendNotification(EventsEnum.USER_SELECTED, View.SelectedUserData);
}
public override IList<string> ListNotificationInterests()
{
IList<string> list = new List<string>();
list.Add(EventsEnum.USER_DELETED);
list.Add(EventsEnum.CANCEL_SELECTED);
list.Add(EventsEnum.USER_ADDED);
list.Add(EventsEnum.USER_UPDATED);
return list;
}
public override void HandleNotification(INotification notification)
{
switch(notification.Name)
{
case EventsEnum.USER_DELETED:
View.Deselect();
View.LoadUsers(userProxy.Users);
break;
case EventsEnum.CANCEL_SELECTED:
View.Deselect();
break;
case EventsEnum.USER_ADDED:
View.Deselect();
View.LoadUsers(userProxy.Users);
break;
case EventsEnum.USER_UPDATED:
View.Deselect();
View.LoadUsers(userProxy.Users);
break;
}
}
}
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;
using System.Collections.Generic;
//更新UserForm部分
public class UserFormMediator : Mediator, IMediator
{
private UserProxy userProxy;
public new const string NAME = "UserFormMediator";
private UserForm View
{
get { return (UserForm)ViewComponent; }
}
public UserFormMediator(UserForm viewComponent)
: base(NAME, viewComponent)
{
Debug.Log("UserFormMediator()");
View.AddUser += UserForm_AddUser;
View.UpdateUser += UserForm_UpdateUser;
View.CancelUser += UserForm_CancelUser;
}
public override void OnRegister()
{
base.OnRegister();
userProxy = Facade.RetrieveProxy(UserProxy.NAME) as UserProxy;
}
void UserForm_AddUser()
{
UserVO user = View.User;
userProxy.AddItem(user);
SendNotification(EventsEnum.USER_ADDED, user);
View.ClearForm();
}
void UserForm_UpdateUser()
{
UserVO user = View.User;
userProxy.UpdateItem(user);
SendNotification(EventsEnum.USER_UPDATED, user);
View.ClearForm();
}
void UserForm_CancelUser()
{
SendNotification(EventsEnum.CANCEL_SELECTED);
View.ClearForm();
}
public override IList<string> ListNotificationInterests()
{
IList<string> list = new List<string>();
list.Add(EventsEnum.NEW_USER);
list.Add(EventsEnum.USER_DELETED);
list.Add(EventsEnum.USER_SELECTED);
return list;
}
public override void HandleNotification(INotification note)
{
UserVO user;
switch (note.Name)
{
case EventsEnum.NEW_USER:
user = (UserVO)note.Body;
View.ShowUser(user, UserFormMode.ADD);
break;
case EventsEnum.USER_DELETED:
View.ClearForm();
break;
case EventsEnum.USER_SELECTED:
user = (UserVO)note.Body;
View.ShowUser(user, UserFormMode.EDIT);
break;
}
}
}
補(bǔ)充,UI的選擇部分
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent(typeof(Toggle))]
public class UGUI_MyToggle : MonoBehaviour
{
[SerializeField]Toggle toggle;
[SerializeField]UGUI_MyToggleGroup myToggleGroup;
void Start()
{
if (toggle == null) toggle = this.GetComponent<Toggle>();
if (myToggleGroup == null) myToggleGroup = toggle.group.GetComponent<UGUI_MyToggleGroup>();
toggle.onValueChanged.AddListener(onToggle);
}
void onToggle(bool value)
{
if(value)
{
myToggleGroup.ChangeToggle(toggle);
}
}
}
//[lzh]
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;
public class ToggleEvent : UnityEvent<Toggle> { }
[RequireComponent(typeof(ToggleGroup))]
public class UGUI_MyToggleGroup : MonoBehaviour
{
public ToggleGroup toggleGroup;
public ToggleEvent onToggleChange = new ToggleEvent();
void Start()
{
if (toggleGroup == null) toggleGroup = this.GetComponent<ToggleGroup>();
}
public void ChangeToggle(Toggle toggle)
{
onToggleChange.Invoke(toggle);
}
}