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

unity3d + PureMVC框架搭建

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

来自: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框架搭建” 的相关文章

线路板设计软件(PCB设计软件)详细综述

    随着电子技术的高速发展,对电子产品的要求越来越高,功能越来越多,虽然蕊片的集成度越来越高,但是,对于线路板的设计要求,也是越来越高的. 线路板设计,也叫PCB设计,因为线路板在英文的全称为 Printed circuit board,简写为PCB,所以线路…

__declspec关键字详细用法

    __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式。其它的有关存储方式的修饰符如static与extern等是C和 C++语言的ANSI规范,而__declspec是一种扩展属性的定义。扩展属性语法简化并标准化了C和C++语言关于M…

UTF-8 and Unicode FAQ

 2000年2月这篇文章说明了在 POSIX 系统 (Linux,Unix) 上使用 Unicode/UTF-8 所需要的信息. 在将来不远的几年里, Unicode 已经很接近于取代 ASCII 与 Latin-1 编码的位置了. 它不仅允许你处理处理事实上存在于地球上的任何语言文…

Android电话功能各部分深入探讨

Android手机操作系统是一款基于Linux平台的开源系统。开发人员可以根据不同的需求对其进行修改等操作。在这系统中有很多比较重要的功能值得我们去研究。比如Android电话功能就是其中一个基础知识点。第一部分 Android电话功能概述Android的Radio Interface Layer…

中国象棋(单机双人javascript版)

中国象棋,对其编写很感兴趣。由于没有感觉去看,或是抽不出时间来去分析。现把代码贴在这里以后做研究。…

关于函数指针

Typedef 声明有助于创建平台无关类型,甚至能隐藏复杂和难以理解的语法。不管怎样,使用 typedef 能为代码带来意想不到的好处,通过本文你可以学习用 typedef 避免缺欠,从而使代码更健壮。 typedef 声明,简称 typedef,为现有类型创建一个新的名字。比如人们常常使用 ty…

发表评论

访客

看不清,换一张

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