来自:http://my.oschina.net/u/1582495/blog/601547
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的启动文件
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); }}
4、在场景中创建一个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 () { }}
5、编写StartupCommand.cs文件,在这里注册所有的command。
using UnityEngine;using System.Collections;using PureMVC.Patterns;public class StartupCommand : MacroCommand { protected override void InitializeMacroCommand () { AddSubCommand (typeof(ModelPreCommand)); }}
5、创建ModelPreCommand.cs文件,这里注册proxy文件。
// 创建Proxy,并注册。public class ModelPreCommand : SimpleCommand { public override void Execute (PureMVC.Interfaces.INotification notification) { Facade.RegisterProxy (new LoginProxy()); }}
6、在AppFacade.cs文件InitializeController方法中注册消息号与Command直接的监听关系。这里使用了NotiConst来定义所有的消息号。
7、创建LoginView.cs这是一个视图文件,同时创建LoginViewMediator.cs文件。
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); }
8、编写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; } }}
9、创建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); }}
10、测试。在视图里面创建一个按钮点击按钮发送登陆消息。
void sendNotice(){ int obj = 233333; AppFacade.getInstance.SendNotification (NotiConst.S_LOGIN,obj);}
然后在写一个接收到服务器端返回数据的操作函数
public void receiveLogin(object obj){ //下一步操作}