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 = ;
AppFacade.getInstance.SendNotification (NotiConst.S_LOGIN,obj);
}

然后在写一个接收到服务器端返回数据的操作函数

 public void receiveLogin(object obj){
//下一步操作
}

原文地址:https://my.oschina.net/u/1582495/blog/601547

Unity3d + PureMVC框架搭建的更多相关文章

  1. Unity3d中PureMVC框架的搭建及使用资料

    1.下载PureMVC框架 https://github.com/PureMVC/puremvc-csharp-multicore-framework https://github.com/PureM ...

  2. Unity 游戏框架搭建 (一) 概述

      为了重构手头的一款项目,翻出来当时未接触Unity时候收藏的视频<Unity项目架构设计与开发管理>,对于我这种初学者来说全是干货.简单的总结了一下,以后慢慢提炼. 关于Unity的架 ...

  3. Unity 游戏框架搭建 (六) 关于框架的一些好文和一些思考

      在进行项目架构阶段,游戏框架可以解决一部分问题.剩下的架构问题还需要根据不同的项目解决.总之游戏框架是游戏架构的一部分. 关于锤子和钉子:   最近又拿起了<代码大全>和<暗时间 ...

  4. SpringMVC笔记——SSM框架搭建简单实例

    落叶枫桥 博客园 首页 新随笔 联系 订阅 管理 SpringMVC笔记——SSM框架搭建简单实例 简介 Spring+SpringMVC+MyBatis框架(SSM)是比较热门的中小型企业级项目开发 ...

  5. Unity 游戏框架搭建 (十一) 简易AssetBundle打包工具(一)

    最近在看Unity官方的AssetBundle(以下简称AB)的教程,也照着做了一遍,不过做出来的AssetBundleManager的API设计得有些不太习惯.目前想到了一个可行的解决方案.AB相关 ...

  6. Unity 游戏框架搭建 (九) 减少加班利器-QConsole

    为毛要实现这个工具? 在我小时候,每当游戏在真机运行时,我们看到的日志是这样的. 没高亮啊,还有乱七八糟的堆栈信息,好干扰日志查看,好影响心情. 还有就是必须始终连着usb线啊,我想要想躺着测试... ...

  7. Angular企业级开发(5)-项目框架搭建

    1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...

  8. 一步一步使用ABP框架搭建正式项目系列教程之本地化详解

    返回总目录<一步一步使用ABP框架搭建正式项目系列教程> 本篇目录 扯扯本地化 ABP中的本地化 小结 扯扯本地化 本节来说说本地化,也有叫国际化.全球化的,不管怎么个叫法,反正道理都是一 ...

  9. ABP框架搭建项目系列教程基础版完结篇

    返回总目录<一步一步使用ABP框架搭建正式项目系列教程> 经过前面十二篇的基础教程,现在终于该做个总结了. 回顾 第一篇,我们建议新手朋友们先通过ABP官网的启动模板生成解决方案,因为这样 ...

随机推荐

  1. [sh]shell案例

    调用同目录下的ip.txt内容: 路径 [root@lanny ~]# pwd /root txt文件 [root@lanny ~]# cat ip.txt 10.1.1.1 10.1.1.2 10. ...

  2. python 使用py2exe将python 脚本生成exe可执行文件

    使用python的py2exe模块可以很容易地帮助我们将python脚本生成可执行的exe程序.这样我们就可以让脚本脱离虚拟机的束缚,从而独立运行. 首先安装py2exe分解步骤如下:(pip和eas ...

  3. 原声js实现addClass removeClass toggleClass效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Tomcat: 8080端口被占用

    用eclipse重启tomcat的时候可能出现这样的错误提示,说明tomcat没有成功地关闭掉. 解决办法:尝试直接用tomcat的shutdown.bat关闭即可,一般都可以解决问题

  5. Filter详解

    转自 http://blog.csdn.net/yudaoai/article/details/4231333 filter功能.它使用户可以改变一个 request和修改一个response. Fi ...

  6. c#写一个网站后台扫描器

    主要分成了那么几个步骤: 1.HTTP状态码 2.字典的导入 3.显示在listview控件当中 第一步: 先来实现HTTP状态码200的判断 try { req = (HttpWebRequest) ...

  7. Redis简述

    Redis 简单介绍 Redis 是全然开源免费的.遵守BSD协议,是一个高性能的key-value数据库. Redis 与其它 key - value 缓存产品有下面三个特点: Redis支持数据的 ...

  8. 【JS】通过JS实现超市小票打印功能——ActiveX控件

    应客户的需求= = ,要在网页端实现打印小票的功能 先来一张打印出的小票效果图(合计明显不对,因为有修改订单功能,请各位忽略) 用什么方法实现呢: 我想应该是有三种吧 1.用第三方的浏览器控件(这个好 ...

  9. mysqld.cc启动分析及运行过程(题目取大了,不好意思)

    mysql源文件的压缩文件mysql-5.7.12.tar.gz有48.2Mb,这么大个家伙. 以前学c语言什么的,觉得尼玛个臭c可以干嘛呀,敢看了源文件我彻底震精了,光是一个THD类的说明就占了30 ...

  10. java日期工具类DateUtil

    一名优秀的程序员,不仅需要有着丰富解决问题的方案,还需要的便是代码的沉淀,这不仅有助于自己快速的开发程序,也有利于保证程序的健壮.那如何才能沉淀自己的”代码“呢?从自己编写util开始其实就是一个不错 ...