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){ //下一步操做 }