0、流程:LoginView-SendNotification()---->LoginCommand--Execute()--->調用proxy中的函數操做模型數據--LoginProxy---->接收服務器返回-操做數據-返回通知視圖控制器--LoginMediator--->操做視圖。c#
(雖然很繁瑣,一個功能須要寫不少個文件,可是對於大型項目來講使用起來是很舒服的。好比A複製揹包,B複製商場,這裏都須要用到人物的金幣信息,對與A/B來講我只要監聽到了金幣更新的操做,我就經過視圖控制器來作update操做就能夠了,不關心是誰的操做引發的金幣變化。好處就很少說了,看下面代碼吧)服務器
一、下載puremvc,http://www.puremvc.org/ mvc
二、複製puremvc源代碼到項目scripts目錄下ide
三、AppFacade.cs文件,這是puremvc的啓動文件函數
using UnityEngine; using System.Collections; using PureMVC.Patterns; using PureMVC.Interfaces; public class AppFacade : Facade,IFacade { public const string STARTUP = "starup"; public const string LOGIN = "login"; private static AppFacade _instance; public static AppFacade getInstance { get{ if (_instance == null) { _instance = new AppFacade (); } return _instance; } } protected override void InitializeController () { base.InitializeController (); RegisterCommand (STARTUP, typeof(StartupCommand)); RegisterCommand (NotiConst.S_LOGIN, typeof(LoginCommand)); } public void startup() { SendNotification (STARTUP); } }
四、在場景中建立一個GameManager.cs文件,掛在Main Camera上測試
using UnityEngine; using System.Collections; public class GameManager : MonoBehaviour { // Use this for initialization void Start () { DontDestroyOnLoad (this.gameObject); AppFacade.getInstance.startup (); } // Update is called once per frame void Update () { } }
五、編寫StartupCommand.cs文件,在這裏註冊全部的command。this
using UnityEngine; using System.Collections; using PureMVC.Patterns; public class StartupCommand : MacroCommand { protected override void InitializeMacroCommand () { AddSubCommand (typeof(ModelPreCommand)); } }
五、建立ModelPreCommand.cs文件,這裏註冊proxy文件。spa
// 建立Proxy,並註冊。 public class ModelPreCommand : SimpleCommand { public override void Execute (PureMVC.Interfaces.INotification notification) { Facade.RegisterProxy (new LoginProxy()); } }
六、在AppFacade.cs文件InitializeController方法中註冊消息號與Command直接的監聽關係。這裏使用了NotiConst來定義全部的消息號。rest
七、建立LoginView.cs這是一個視圖文件,同時建立LoginViewMediator.cs文件。code
using UnityEngine; using System.Collections; using System.Collections.Generic; using PureMVC.Patterns; using PureMVC.Interfaces; public class LoginViewMediator : Mediator,IMediator { public const string NAME = "LoginViewMediator"; public LoginViewMediator(LoginView _view):base(NAME,_view){ } //須要監聽的消息號 public override System.Collections.Generic.IList<string> ListNotificationInterests () { List<string> list = new List<string>(); list.Add (NotiConst.R_LOGIN); return list; } //接收消息到消息以後處理 public override void HandleNotification (PureMVC.Interfaces.INotification notification) { string name = notification.Name; object vo = notification.Body; switch (name) { case NotiConst.R_LOGIN: (this.ViewComponent as LoginView).receiveMessage (vo); break; } } }
LoginView.cs
void Start () { //註冊mediator AppFacade.getInstance.RegisterMediator (new LoginViewMediator (this)); } void OnDestory(){ AppFacade.getInstance.RemoveMediator (LoginViewMediator.NAME); }
八、編寫LoginCommand.cs文件,監聽發送過來的消息。
在AppFacade裏面InitializeController註冊:RegisterCommand (NotiConst.S_LOGIN, typeof(LoginCommand));
using UnityEngine; using System.Collections; using PureMVC.Patterns; public class LoginCommand : SimpleCommand { public override void Execute (PureMVC.Interfaces.INotification notification) { Debug.Log ("LoginCommand"); object obj = notification.Body; LoginProxy loginProxy; loginProxy = Facade.RetrieveProxy (LoginProxy.NAME) as LoginProxy; string name = notification.Name; switch (name) { case NotiConst.S_LOGIN: loginProxy.sendLogin (obj); break; } } }
九、建立LoginProxy.cs文件,這裏複製數據處理,與服務器通信等操做。
using UnityEngine; using System.Collections; using PureMVC.Patterns; using PureMVC.Interfaces; using LitJson; public class LoginProxy : Proxy,IProxy { public const string NAME = "LoginProxy"; // Use this for initialization public LoginProxy():base(NAME){} //請求登錄 public void sendLogin(object data) { //與服務器通信,返回消息處理玩以後,若是須要改變試圖則調用下面消息 receiveLogin(); } // 登錄返回 private void receiveLogin(JsonData rData) { SendNotification (NotiConst.R_LOGIN, rData); } }
十、測試。在視圖裏面建立一個按鈕點擊按鈕發送登錄消息。
void sendNotice(){ int obj = 233333; AppFacade.getInstance.SendNotification (NotiConst.S_LOGIN,obj); }
而後在寫一個接收到服務器端返回數據的操做函數
public void receiveLogin(object obj){ //下一步操做 }