PureMVC和Unity3D的UGUI制作一個簡單的員工管理系統實例


前言:

1.關於PureMVC:

MVC框架在很多項目當中擁有廣泛的應用,很多時候做項目前人開坑開了一半就消失了,后人為了填補各種的坑就遭殃的不得了。嘛,程序猿大家都不喜歡像文案策划一樣組織文字寫東西,也不喜歡看別人留下不清不楚的文檔,還不如看代碼來得實在。剛開始新人看代碼是看得頭暈,因為這樣那樣的東西不一定能完全猜得透。而老人家就算有經驗和閱歷,也沒有這么多的體力去看一個龐大而又復雜的糟糕項目。因為這種需求,Unity3D的程序猿就統一組織起來,想做一個完整規范的程序框架,而這時,PureMVC就誕生了。我個人喜歡PureMVC的原因也很簡單,因為它簡單粗暴,和Unity3D之間沒有依賴性,加上又開源,真的遇到Bug能拿到源代碼來調試也是很容易執行的。Unity3D應用商店還有一個同類產品叫uFrame Game Framework,它對Unity版本有着依賴,拖視圖雖然方便,但是一旦出bug真的改得夠嗆的,所以不推薦使用它。下文便是使用PureMVC和Unity3D的UGUI制作一個簡單的員工管理系統實例。

2.通過MVC模式在Unity項目當中應用的特別提醒:

(1)Unity3D是基於組件設計的,如果沒有好的規划,組件之間會產生復雜的調用關系,導致組件之間復雜的依賴,從而破壞了整個系統結構,因此需要設計時確定組件的層次關系,確保依賴關系只存在於下層對上層。而這個是業務邏輯設計問題,PureMVC幫不了你。
(2)僅僅用上MVC,解決不了什么問題,或許解決了1%,剩下的99%就被挪到了MVC的C里,當你慶祝MVC竣工時,99%的問題在那里默默的微笑的看着你。(話說以前寫PHP的CI框架時候,一堆東西扔到XxxAction.Class.php里面,發現和擺的亂七八糟的架構沒區別,只是大家都習慣了這套框架的規矩,看代碼找某個東西稍微好找而已,本質上還是考驗基本功和對項目的熟悉程度的,23333)

3.PureMVC通過4種pattern實現隔離變化:

(1)facade非常適合將UI界面對游戲數據的依賴解耦,將UI操作數據的請求封裝在facade 接口里面,同時數據變化對UI的影響可以通過notification事件通知來實現,該模式應用得非常常見。
(2)command模式統一對對象的操作,將鍵盤輸入,網絡輸入輸出統一成command來操控游戲對象。
(3)proxy維護數據,提供某塊數據統一的初始化,訪問和修改接口。
(4)mediator沒怎么用過,我們的游戲中的UI界面每次變化一般都是整體更新的,不常用局部更新。
以上4中pattern請務必牢牢記住,請務必牢牢記住,請務必牢牢記住。重要的事情要說3便。

4.PureMVC的流程示意圖

(1)在puremvc中,model/view/controller統一是由Facade類的單件實例來統籌管理的。
(2)PureMVC的基本流程:啟動PureMVC—>建立Mediator來操作視覺元素(按鈕與文本框)—>點擊按鈕發送Notification->文本框接收Notification改變內容。
(3)大致流程可理解為:通過Facade類的單件實例(即:統一的門面) 啟動 puremvc環境,啟動同時注冊Command對象(相當於asp.net mvc中的controller),然后Command通過與之關聯的facade(即前面的單件實例)來注冊Mediator(中介者:用於把View與Command聯系起來)。
(4)當UI界面(即View)上有動靜時(比如按鈕點擊了之類),與之關聯的Mediator(中介者)會發送通知給facade,然后facade會調用command對象執行相關的處理。(即:消息響應)


PureMVC示意圖.png

一.引入PureMVC的插件

1.下載PureMVC

請訪問地址
https://github.com/PureMVC/puremvc-csharp-standard-framework/wiki

安裝

2.把PureMVC.DotNET.35.dll放到Plugins里面就好了。

QA

3.這里有一個很簡單的基本案例可以參考一下
http://www.open-open.com/lib/view/open1452657515480.html

二.動手配置文件

1.需要完成的實例如下:


實現的界面效果.png

2.具體實現的目標:

(1)在Scripts文件夾下,分別設置模型、視圖、控制器對應的文件夾Model、View、Controller,分別放置處理數據模型的腳本、處理顯示視圖的腳本、處理邏輯控制的腳本。
(2)如界面,一個Unity3D和UGUI制作的簡單員工管理系統,Employee Admin,其中員工界面Users顯示有哪些員工在登記范圍內,而New和Delete分別是添加和刪除某個員工的信息。然后下面的員工信息界面User Profile則是對員工信息的一個具體編輯和修正。

三.主要實現步驟

1.啟動文件AppFacade.cs 作為PureMVC框架的入口文件。

 1 using UnityEngine;
 2 using System.Collections;
 3 using PureMVC.Patterns;
 4 using PureMVC.Interfaces;
 5 
 6 //Facade模式的單例
 7 public class ApplicationFacade : Facade
 8 {
 9     //實例化函數,保證單例模式(Singleton)運行該函數
10     public new static IFacade Instance
11     {
12         get
13         {
14             if(m_instance == null)
15             {
16                 lock(m_staticSyncRoot)
17                 {
18                     if (m_instance == null)
19                     {
20                         Debug.Log("ApplicationFacade");
21                         m_instance = new ApplicationFacade();
22                     }
23                 }
24             }
25             return m_instance;
26         }
27     }
28     //啟動PureMVC的入口函數
29     public void Startup(MainUI mainUI)
30     {
31         Debug.Log("Startup() to SendNotification.");
32         SendNotification(EventsEnum.STARTUP, mainUI);
33     }
34     //該類的構造器
35     protected ApplicationFacade()
36     {
37 
38     }
39     //設置靜態
40     static ApplicationFacade()
41     {
42 
43     }
44     //初始化控制器函數
45     protected override void InitializeController()
46     {
47         Debug.Log("InitializeController()");
48         base.InitializeController();
49         RegisterCommand(EventsEnum.STARTUP, typeof(StartupCommand));
50         RegisterCommand(EventsEnum.DELETE_USER, typeof(DeleteUserCommand));
51     }
52 }

2.對PureMVC需要處理的事件用EventsEnum.cs存放

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 //處理事件的枚舉
 5 public class EventsEnum
 6 {
 7     public const string STARTUP = "startup";//啟動事件
 8 
 9     public const string NEW_USER = "newUser";//新建用戶
10     public const string DELETE_USER = "deleteUser";//刪除用戶
11     public const string CANCEL_SELECTED = "cancelSelected";//取消選擇
12 
13     public const string USER_SELECTED = "userSelected";//選擇用戶
14     public const string USER_ADDED = "userAdded";//添加用戶
15     public const string USER_UPDATED = "userUpdated";//更新用戶
16     public const string USER_DELETED = "userDeleted";//刪除用戶
17 
18     public const string ADD_ROLE = "addRole";//添加角色
19     public const string ADD_ROLE_RESULT = "addRoleResult";//查詢添加角色的結果
20 }

3.然后在Unity的場景中創建一個MainUI.cs文件,掛在需要啟動PureMVC的組件上。就可以啟動了。

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 //處理該UI場景的入口
 5 public class MainUI : MonoBehaviour
 6 {
 7     public UserList userList;
 8     public UserForm userForm;
 9     //啟動函數
10     void Awake()
11     {
12         //啟動PureMVC程序,執行StartUP()方法
13         ApplicationFacade facade = ApplicationFacade.Instance as ApplicationFacade;
14         facade.Startup(this);
15     }
16 }

4.對Controller部分進行處理

然后我們對執行邏輯的處理事件進行補充。新建一個文件夾Controller,暫時先放置StartupCommand.cs和DeleteUserCommand.cs。處理上述所說的邏輯事件
首先,處理啟動事件

 1 using UnityEngine;
 2 using System.Collections;
 3 using PureMVC.Patterns;
 4 using PureMVC.Interfaces;
 5 
 6 //啟動事件
 7 public class StartupCommand : SimpleCommand, ICommand
 8 {
 9     //復寫原有的Execute執行函數
10     public override void Execute(INotification notification)
11     {
12         //注冊統一的數據接口UserProxy,給其他事件處理
13         Debug.Log("StartupCommand.Execute()");
14         Facade.RegisterProxy(new UserProxy());
15 
16         //注冊局部界面Mediator,給其他事件處理
17         MainUI mainUI = notification.Body as MainUI;
18         Facade.RegisterMediator(new UserListMediator(mainUI.userList));
19         Facade.RegisterMediator(new UserFormMediator(mainUI.userForm));
20     }
21 }

其次,處理刪除用戶事件

 1 using PureMVC.Patterns;
 2 using PureMVC.Interfaces;
 3 
 4 //刪除用戶事件
 5 public class DeleteUserCommand : SimpleCommand, ICommand
 6 {
 7    //復寫原有的Execute執行函數
 8     public override void Execute(INotification notification)
 9     {
10         //獲取要刪除的對象user
11         UserVO user = (UserVO)notification.Body;
12         //獲取處理數據操作的userProxy
13         UserProxy userProxy = (UserProxy)Facade.RetrieveProxy(UserProxy.NAME);
14 
15         //操作數據,刪除user
16         userProxy.DeleteItem(user);
17         //刪除完畢,廣播USER_DELETED進行通知
18         SendNotification(EventsEnum.USER_DELETED);
19     }
20 }

5.對Model部分進行處理

然后是對Model模型數據的定義哈,首先要記錄的信息用UserVO來表示

 1 using UnityEngine;
 2 using System.Collections;
 3 //顯示用的數據信息UserViewObject
 4 public class UserVO
 5 {
 6     //用戶名
 7     public string UserName
 8     {
 9         get { return m_userName; }
10     }
11     private string m_userName = "";
12     //名字
13     public string FirstName
14     {
15         get { return m_firstName; }
16     }
17     private string m_firstName = "";
18     //姓氏
19     public string LastName
20     {
21         get { return m_lastName; }
22     }
23     private string m_lastName = "";
24     //郵箱
25     public string Email
26     {
27         get { return m_email; }
28     }
29     private string m_email = "";
30     //密碼
31     public string Password
32     {
33         get { return m_password; }
34     }
35     private string m_password = "";
36     //部門
37     public string Department
38     {
39         get { return m_department; }
40     }
41     private string m_department = "";
42     //是否合法
43     public bool IsValid
44     {
45         get
46         {
47             return !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password);
48         }
49     }
50     //合並名字
51     public string GivenName
52     {
53         get { return LastName + ", " + FirstName; }
54     }
55     //構造器
56     public UserVO()
57     {
58     }
59     //構造函數
60     public UserVO(string uname, string fname, string lname, string email, string password, string department)
61     {
62         if (uname != null) m_userName = uname;
63         if (fname != null) m_firstName = fname;
64         if (lname != null) m_lastName = lname;
65         if (email != null) m_email = email;
66         if (password != null) m_password = password;
67         if (department != null) m_department = department;
68     }
69 }

接着對操作數據的UserProxy進行補充

 1 using UnityEngine;
 2 using System.Collections.Generic;
 3 using PureMVC.Patterns;
 4 using PureMVC.Interfaces;
 5 
 6 //數據操作
 7 public class UserProxy : Proxy, IProxy
 8 {
 9     public new const string NAME = "UserProxy";
10 
11     //返回數據操作類
12     public IList<UserVO> Users
13     {
14         get { return (IList<UserVO>) base.Data; }
15     }
16 
17     //構造函數,添加幾條數據進去先
18     public UserProxy():base(NAME, new List<UserVO>())
19     {
20         Debug.Log("UserProxy()");
21         //添加幾條測試用的數據        
22         AddItem(new UserVO("lstooge", "Larry", "Stooge", "larry@stooges.com", "ijk456", "ACCT"));
23         AddItem(new UserVO("cstooge", "Curly", "Stooge", "curly@stooges.com", "xyz987", "SALES"));
24         AddItem(new UserVO("mstooge", "Moe", "Stooge", "moe@stooges.com", "abc123", "PLANT"));
25         AddItem(new UserVO("lzh", "abc", "def", "lzh@stooges.com", "abc123", "IT"));
26     }
27 
28     //添加數據的方法
29     public void AddItem(UserVO user)
30     {
31         Users.Add(user);
32     }
33 
34     //更新數據的方法
35     public void UpdateItem(UserVO user)
36     {
37         for (int i = 0; i < Users.Count; i++)
38         {
39             if (Users[i].UserName.Equals(user.UserName))
40             {
41                 Users[i] = user;
42                 break;
43             }
44         }
45     }
46 
47     //刪除數據的方法
48     public void DeleteItem(UserVO user)
49     {
50         for (int i = 0; i < Users.Count; i++)
51         {
52             if (Users[i].UserName.Equals(user.UserName))
53             {
54                 Users.RemoveAt(i);
55                 break;
56             }
57         }
58     }
59 }

6.接下來對VIew部分進行實現

顯示用戶界面列表的UserList和填寫用戶具體信息的UserForm
列表單條數據部分

 1 using UnityEngine;
 2 using UnityEngine.UI;
 3 using System.Collections;
 4 
 5 //UserList列表當中單條數據信息
 6 public class UserList_Item : MonoBehaviour
 7 {
 8     //定義UI組件
 9     public Text txt_userName;//用戶名文本框
10     public Text txt_firstName;//名字文本框
11     public Text txt_lastName;//姓氏文本框
12     public Text txt_email;//郵件文本框
13     public Text txt_department;//部門文本框
14 
15     //定義User信息類
16     public UserVO userData;
17 
18     //更新User類
19     public void UpdateData(UserVO data)
20     {
21         //獲取需要更改的User信息
22         this.userData = data;
23 
24         //更改UI的文字信息
25         txt_userName.text = data.UserName;
26         txt_firstName.text = data.FirstName;
27         txt_lastName.text = data.LastName;
28         txt_email.text = data.Email;
29         txt_department.text = data.Department;
30     }
31 }

用戶列表部分

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;//用戶列表滑動條
    public Button btn_New;//新建按鈕
    public Button btn_Delete;//刪除按鈕
    public UserList_Item itemPrefab;//單個列表文件的預置體
    List<UserList_Item> itemList = new List<UserList_Item>();//臨時存列表

    //定義事件操作
    public System.Action NewUser;//添加用戶事件
    public System.Action DeleteUser;//刪除用戶事件
    public System.Action SelectUser;//選擇用戶事件

    //定義數據
    public UserVO SelectedUserData;//列表中選擇好的用戶
    private IList<UserVO> m_currentUsers;//當前選擇的用戶

    //開始函數
    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);
    }
    //點擊新建
    void onClick_btn_New()
    {
        if (NewUser != null) NewUser();
    }
    //點擊刪除
    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();
    }
    //新建列表項目
    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);
    }
}

用戶個人信息部分

  1 using UnityEngine;
  2 using UnityEngine.UI;
  3 using System.Collections;
  4 using UnityEngine.EventSystems;
  5 
  6 //用戶個人信息表單模式?編輯:新增
  7 public enum UserFormMode
  8 {
  9     ADD,
 10     EDIT,
 11 }
 12 //用戶個人信息表單
 13 public class UserForm : MonoBehaviour
 14 {
 15     //UI項的定義
 16     public InputField txt_firstName;
 17     public InputField txt_lastName;
 18     public InputField txt_email;
 19     public InputField txt_userName;
 20     public InputField txt_password;
 21     public InputField txt_confirmPassword;
 22     public InputField txt_department;
 23     public Button btn_updateUser;
 24     public Button btn_cancel;
 25 
 26     //其他
 27     public System.Action AddUser;
 28     public System.Action UpdateUser;
 29     public System.Action CancelUser;
 30 
 31     //用戶信息獲取
 32     public UserVO User
 33     {
 34         get { return m_user; }
 35     }
 36     private UserVO m_user;
 37 
 38     //用戶信息表單
 39     public UserFormMode Mode
 40     {
 41         get { return m_mode; }
 42     }
 43     private UserFormMode m_mode = UserFormMode.ADD;
 44 
 45     //開始
 46     void Start ()
 47     {
 48         //設置UI
 49         btn_updateUser.onClick.AddListener(btn_updateUser_Click);
 50         btn_cancel.onClick.AddListener(btn_cancel_Click);
 51 
 52         txt_userName.onValueChange.AddListener(InputField_onValueChange);
 53         txt_password.onValueChange.AddListener(InputField_onValueChange);
 54         txt_confirmPassword.onValueChange.AddListener(InputField_onValueChange);
 55 
 56         UpdateButtons();
 57     }
 58 
 59     //顯示當前用戶信息
 60     public void ShowUser(UserVO user, UserFormMode mode)
 61     {
 62         m_mode = mode;
 63         if (user == null)
 64         {
 65             ClearForm();
 66         }
 67         else
 68         {
 69             m_user = user;
 70             txt_firstName.text = user.FirstName;
 71             txt_lastName.text = user.LastName;
 72             txt_email.text = user.Email;
 73             txt_userName.text = user.UserName;
 74             txt_password.text = txt_confirmPassword.text = user != null ? user.Password : "";
 75             txt_department.text = user.Department;
 76             //txt_firstName.ActivateInputField();
 77             EventSystem.current.SetSelectedGameObject(txt_firstName.gameObject);
 78             //txt_firstName.MoveTextEnd(false);
 79             txt_firstName.caretPosition = txt_firstName.text.Length - 1;
 80             UpdateButtons();
 81         }
 82     }
 83 
 84     //更新按鈕
 85     private void UpdateButtons()
 86     {
 87         if (btn_updateUser != null)
 88         {
 89             btn_updateUser.interactable = (txt_firstName.text.Length > 0 && txt_password.text.Length > 0 && txt_password.text.Equals(txt_confirmPassword.text));
 90         }
 91     }
 92 
 93     //清空表單
 94     public void ClearForm()
 95     {
 96         m_user = null;
 97         txt_firstName.text = txt_lastName.text = txt_email.text = txt_userName.text = "";
 98         txt_password.text = txt_confirmPassword.text = "";
 99         txt_department.text = "";
100         UpdateButtons();
101     }
102 
103     //更新用戶信息
104     void btn_updateUser_Click()
105     {
106         m_user = new UserVO(
107             txt_userName.text, txt_firstName.text, 
108             txt_lastName.text, txt_email.text, 
109             txt_password.text, txt_department.text);
110 
111         if (m_user.IsValid)
112         {
113             if (m_mode == UserFormMode.ADD)
114             {
115                 if (AddUser != null) AddUser();
116             }
117             else
118             {
119                 if (UpdateUser != null) UpdateUser();
120             }
121         }
122     }
123 
124     //取消信息
125     void btn_cancel_Click()
126     {
127         if (CancelUser != null) CancelUser();
128     }
129 
130     //輸入
131     void InputField_onValueChange(string value)
132     {
133         UpdateButtons();
134     }
135 }

最后,對局部更新UI的行為進行完善Mediator

 1 using UnityEngine;
 2 using System.Collections;
 3 using PureMVC.Patterns;
 4 using PureMVC.Interfaces;
 5 using System.Collections.Generic;
 6 //更新UserList部分
 7 public class UserListMediator : Mediator, IMediator
 8 {
 9     private UserProxy userProxy;
10 
11     public new const string NAME = "UserListMediator";
12 
13     private UserList View
14     {
15         get { return (UserList)ViewComponent; }
16     }
17 
18     public UserListMediator(UserList userList)
19             : base(NAME, userList)
20     {
21         Debug.Log("UserListMediator()");
22         userList.NewUser += userList_NewUser;
23         userList.DeleteUser += userList_DeleteUser;
24         userList.SelectUser += userList_SelectUser;
25     }
26 
27     public override void OnRegister()
28     {
29         Debug.Log("UserListMediator.OnRegister()");
30         base.OnRegister();
31         userProxy = Facade.RetrieveProxy(UserProxy.NAME) as UserProxy;
32         View.LoadUsers(userProxy.Users);
33     }
34 
35     void userList_NewUser()
36     {
37         UserVO user = new UserVO();
38         SendNotification(EventsEnum.NEW_USER, user);
39     }
40 
41     void userList_DeleteUser()
42     {
43         SendNotification(EventsEnum.DELETE_USER, View.SelectedUserData);
44     }
45 
46     void userList_SelectUser()
47     {
48         SendNotification(EventsEnum.USER_SELECTED, View.SelectedUserData);
49     }
50 
51     public override IList<string> ListNotificationInterests()
52     {
53         IList<string> list = new List<string>();
54         list.Add(EventsEnum.USER_DELETED);
55         list.Add(EventsEnum.CANCEL_SELECTED);
56         list.Add(EventsEnum.USER_ADDED);
57         list.Add(EventsEnum.USER_UPDATED);
58         return list;
59     }
60 
61     public override void HandleNotification(INotification notification)
62     {
63         switch(notification.Name)
64         {
65             case EventsEnum.USER_DELETED:
66                 View.Deselect();
67                 View.LoadUsers(userProxy.Users);
68                 break;
69             case EventsEnum.CANCEL_SELECTED:
70                 View.Deselect();
71                 break;
72             case EventsEnum.USER_ADDED:
73                 View.Deselect();
74                 View.LoadUsers(userProxy.Users);
75                 break;
76             case EventsEnum.USER_UPDATED:
77                 View.Deselect();
78                 View.LoadUsers(userProxy.Users);
79                 break;
80         }
81     }
82 }
 1 using UnityEngine;
 2 using System.Collections;
 3 using PureMVC.Patterns;
 4 using PureMVC.Interfaces;
 5 using System.Collections.Generic;
 6 //更新UserForm部分
 7 public class UserFormMediator : Mediator, IMediator
 8 {
 9     private UserProxy userProxy;
10 
11     public new const string NAME = "UserFormMediator";
12 
13     private UserForm View
14     {
15         get { return (UserForm)ViewComponent; }
16     }
17 
18     public UserFormMediator(UserForm viewComponent)
19         : base(NAME, viewComponent)
20     {
21         Debug.Log("UserFormMediator()");
22 
23         View.AddUser += UserForm_AddUser;
24         View.UpdateUser += UserForm_UpdateUser;
25         View.CancelUser += UserForm_CancelUser;
26     }
27 
28     public override void OnRegister()
29     {
30         base.OnRegister();
31         userProxy = Facade.RetrieveProxy(UserProxy.NAME) as UserProxy;
32     }
33 
34     void UserForm_AddUser()
35     {
36         UserVO user = View.User;
37         userProxy.AddItem(user);
38         SendNotification(EventsEnum.USER_ADDED, user);
39         View.ClearForm();
40     }
41 
42     void UserForm_UpdateUser()
43     {
44         UserVO user = View.User;
45         userProxy.UpdateItem(user);
46         SendNotification(EventsEnum.USER_UPDATED, user);
47         View.ClearForm();
48     }
49 
50     void UserForm_CancelUser()
51     {
52         SendNotification(EventsEnum.CANCEL_SELECTED);
53         View.ClearForm();
54     }
55 
56     public override IList<string> ListNotificationInterests()
57     {
58         IList<string> list = new List<string>();
59         list.Add(EventsEnum.NEW_USER);
60         list.Add(EventsEnum.USER_DELETED);
61         list.Add(EventsEnum.USER_SELECTED);
62         return list;
63     }
64 
65     public override void HandleNotification(INotification note)
66     {
67         UserVO user;
68         switch (note.Name)
69         {
70             case EventsEnum.NEW_USER:
71                 user = (UserVO)note.Body;
72                 View.ShowUser(user, UserFormMode.ADD);
73                 break;
74 
75             case EventsEnum.USER_DELETED:
76                 View.ClearForm();
77                 break;
78 
79             case EventsEnum.USER_SELECTED:
80                 user = (UserVO)note.Body;
81                 View.ShowUser(user, UserFormMode.EDIT);
82                 break;
83 
84         }
85     }
86 }

補充,UI的選擇部分

 1 using UnityEngine;
 2 using UnityEngine.UI;
 3 using System.Collections;
 4 
 5 [RequireComponent(typeof(Toggle))]
 6 public class UGUI_MyToggle : MonoBehaviour
 7 {    
 8     [SerializeField]Toggle toggle;
 9     [SerializeField]UGUI_MyToggleGroup myToggleGroup;
10 
11     void Start()
12     {
13         if (toggle == null) toggle = this.GetComponent<Toggle>();
14         if (myToggleGroup == null) myToggleGroup = toggle.group.GetComponent<UGUI_MyToggleGroup>();
15 
16         toggle.onValueChanged.AddListener(onToggle);
17     }
18 
19     void onToggle(bool value)
20     {
21         if(value)
22         {
23             myToggleGroup.ChangeToggle(toggle);
24         }
25     }
26 }

原文地址:http://www.jianshu.com/p/904b36ad37e2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM