Unity3d + PureMVC框架搭建


0、流程:LoginView-SendNotification()---->LoginCommand--Execute()--->調用proxy中的函數操作模型數據--LoginProxy---->接收服務器返回-操作數據-返回通知視圖控制器--LoginMediator--->操作視圖。

(雖然很繁瑣,一個功能需要寫很多個文件,但是對於大型項目來說使用起來是很舒服的。比如A復制背包,B復制商場,這里都需要用到人物的金幣信息,對與A/B來說我只要監聽到了金幣更新的操作,我就通過視圖控制器來做update操作就可以了,不關心是誰的操作引起的金幣變化。好處就不多說了,看下面代碼吧)

 

1、下載puremvc,http://www.puremvc.org/

2、復制puremvc源代碼到項目scripts目錄下

3、AppFacade.cs文件,這是puremvc的啟動文件

 1 using UnityEngine;
 2 using System.Collections;
 3 using PureMVC.Patterns;
 4 using PureMVC.Interfaces;
 5 public class AppFacade : Facade,IFacade {
 6     public const string STARTUP = "starup";
 7     public const string LOGIN = "login";
 8     private static AppFacade _instance;
 9     public static AppFacade getInstance
10     {
11         get{ 
12             if (_instance == null) {
13                 _instance = new AppFacade ();
14             }
15             return _instance;
16         }
17     }
18     protected override void InitializeController ()
19     {
20         base.InitializeController ();
21         RegisterCommand (STARTUP, typeof(StartupCommand));
22         RegisterCommand (NotiConst.S_LOGIN, typeof(LoginCommand));
23     }
24     public void startup()
25     {
26         SendNotification (STARTUP);
27     }
28 }

4、在場景中創建一個GameManager.cs文件,掛在Main Camera上

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class GameManager : MonoBehaviour {
 5 
 6     // Use this for initialization
 7     void Start () {
 8         DontDestroyOnLoad (this.gameObject);
 9         AppFacade.getInstance.startup ();
10     }
11     
12     // Update is called once per frame
13     void Update () {
14     
15     }
16 }

5、編寫StartupCommand.cs文件,在這里注冊所有的command。

 1 using UnityEngine;
 2 using System.Collections;
 3 using PureMVC.Patterns;
 4 
 5 public class StartupCommand : MacroCommand {
 6     protected override void InitializeMacroCommand ()
 7     {
 8         AddSubCommand (typeof(ModelPreCommand));
 9     }
10 
11 }

5、創建ModelPreCommand.cs文件,這里注冊proxy文件。

1 // 創建Proxy,並注冊。
2 public class ModelPreCommand : SimpleCommand {
3 
4     public override void Execute (PureMVC.Interfaces.INotification notification)
5     {
6         Facade.RegisterProxy (new LoginProxy());
7     }
8 }

6、在AppFacade.cs文件InitializeController方法中注冊消息號與Command直接的監聽關系。這里使用了NotiConst來定義所有的消息號。

7、創建LoginView.cs這是一個視圖文件,同時創建LoginViewMediator.cs文件。

 1 using UnityEngine;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using PureMVC.Patterns;
 5 using PureMVC.Interfaces;
 6 
 7 public class LoginViewMediator : Mediator,IMediator {
 8 
 9     public const string NAME = "LoginViewMediator";
10 
11     public LoginViewMediator(LoginView _view):base(NAME,_view){
12         
13     }
14     //需要監聽的消息號
15     public override System.Collections.Generic.IList<string> ListNotificationInterests ()
16     {
17         List<string> list = new List<string>();
18         list.Add (NotiConst.R_LOGIN);
19         return list;
20     }
21     //接收消息到消息之后處理
22     public override void HandleNotification (PureMVC.Interfaces.INotification notification)
23     {
24         string name = notification.Name;
25         object vo = notification.Body;
26         switch (name) {
27         case NotiConst.R_LOGIN:
28                 (this.ViewComponent as LoginView).receiveMessage (vo);
29                 break;
30         }
31     }
32 }

LoginView.cs

1 void Start () {
2 //注冊mediator
3         AppFacade.getInstance.RegisterMediator (new LoginViewMediator (this));
4     }
5 
6 void OnDestory(){
7         AppFacade.getInstance.RemoveMediator (LoginViewMediator.NAME);
8     }

8、編寫LoginCommand.cs文件,監聽發送過來的消息。

在AppFacade里面InitializeController注冊:RegisterCommand (NotiConst.S_LOGIN, typeof(LoginCommand));

 1 using UnityEngine;
 2 using System.Collections;
 3 using PureMVC.Patterns;
 4 
 5 public class LoginCommand : SimpleCommand {
 6 
 7     public override void Execute (PureMVC.Interfaces.INotification notification)
 8     {
 9         Debug.Log ("LoginCommand");
10         object obj = notification.Body;
11         LoginProxy loginProxy;
12         loginProxy = Facade.RetrieveProxy (LoginProxy.NAME) as LoginProxy;
13         string name = notification.Name;
14         switch (name) {
15         case NotiConst.S_LOGIN:
16             loginProxy.sendLogin (obj);
17             break;
18         }
19     }
20 }

9、創建LoginProxy.cs文件,這里復制數據處理,與服務器通訊等操作。

 1 using UnityEngine;
 2 using System.Collections;
 3 using PureMVC.Patterns;
 4 using PureMVC.Interfaces;
 5 using LitJson;
 6 
 7 public class LoginProxy : Proxy,IProxy {
 8     public const string NAME = "LoginProxy";
 9     // Use this for initialization
10     public LoginProxy():base(NAME){}
11     //請求登陸
12     public void sendLogin(object data)
13     {
14         //與服務器通訊,返回消息處理玩之后,如果需要改變試圖則調用下面消息
15         receiveLogin();
16     }
17     // 登陸返回
18     private void receiveLogin(JsonData rData)
19     {
20        SendNotification (NotiConst.R_LOGIN, rData);
21     }
22 }

10、測試。在視圖里面創建一個按鈕點擊按鈕發送登陸消息。

1 void sendNotice(){
2     int obj = 233333;
3     AppFacade.getInstance.SendNotification (NotiConst.S_LOGIN,obj);
4 }

然后在寫一個接收到服務器端返回數據的操作函數

1 public void receiveLogin(object obj){
2     //下一步操作
3 }

原文地址:https://my.oschina.net/u/1582495/blog/601547


免責聲明!

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



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