当前位置:首页 > 编程开发

unity3d + PureMVC框架搭建

webgou10年前 (2017-02-22)编程开发309

来自: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){    //下一步操作}
 

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=601

标签: unity3d
分享给朋友:

“unity3d + PureMVC框架搭建” 的相关文章

Android NDK开发指南(二)Android.mk文件

Android.mk文件语法详述...…

fatal error C1853 预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)

   C1853 编译器错误:fatal error C1853: 'pjtname.pch' precompiled header file is from a previous version of the compiler, 解决办法…

软件测试的分类

A.白盒测试…

线程

线程是由两个部分组成的: * 一个是线程的内核对象,操作系统用它来对线程实施管理。内核对象也是系统用来存放 线程统计信息的地方。 * 另一个是线程堆栈,它用于维护线程在执行代码时需要的所有函数参数和局部变量 进程是不活泼的。进程从来不执行任何东西,它只是线程的…

DirectShow中常见的RGB/YUV格式

 DirectShow中常见的RGB/YUV …

第一个win32程序

#include "stdafx.h" LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstatn…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。