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的啟動文件
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){
//下一步操作
}