这两天看了下老大的项目,他基本都是用MVC模式,写的很好,在此把我理解的记录下来

Model:实体对象(对应数据库记录的类)

View:视图

presenter(controller):业务处理

view中有present对象,present中有model和view对象

view中UI的交互会调用present相应处理的方法(该方法处理完后会调用view显示处理后的视图(将改变后的model对象传递过去),view显示就会改变显示的内容)

结构是下面这样子的:

IPresenter:

namespace NginAR
{
public interface IPresenter<T> where T : class
{
void Init();
}
}

IView:

namespace NginAR
{
public interface IView<T> where T : class
{
/**
* 设置Presenter
* @param presenter
*/
void setPresenter(T presenter);
}
}

IGlassView:

namespace NginAR
{
public interface IGlassView : IView<IGlassPresenter>
{
// 设置当前步骤内容,用于显示
void setStepContent(string content);
// 设置当前步骤
void setCurrentStep(int currentStep);
}
public interface IGlassPresenter : IPresenter<IGlassView>
{
// 当前步
bool StepCurrent();
// 下一步
bool StepNext();
// 上一步
bool StepLast();
void onStepChange(int currentTaskId, int currentStepId);
}
}

Step:

namespace NginAR
{
public class Step
{
public int id { get; set; }
public string content { get; set; }
public string type { get; set; }
}
}

FileUtil:

namespace NginAR
{
// 读取文件工具
public class FileUtil
{
// 加载脚本文件,用于保存步骤内容
public static void LoadModel(TextAsset asset,List<Step> steps) {
string[] lines = asset.text.Split("\n"[]);
for (int i = ; i < lines.Length; i++)
{
string[] cols = lines[i].Split(" "[]);
Step step = new Step();
step.id = int.Parse(cols[]);
step.content = cols[];
step.type = cols[]; steps.Add(step);
}
LogUtil.i("大小"+ steps.Count);
}
} }

LogUtil:

public class LogUtil
{
private static bool LOGI = true;
private static bool LOGW = true;
private static bool LOGE = true; public static void i(string mess)
{
if (LOGI)
{
Debug.Log(mess);
}
} public static void w(string mess)
{
if (LOGW)
{
Debug.LogWarning(mess);
}
} public static void e(string mess)
{
if (LOGE)
{
Debug.LogError(mess);
}
}
}

MainView(继承IGlassView):

UI事件调用present中对应的处理方法:

   // 新建逻辑管理对象 mvp-p
prenseter = new MainPresenter(asset, this);
// 为刷新当前步骤按钮设置监听回调
btn_text.onClick.AddListener(delegate () {
prenseter.StepCurrent();
});
// 为上一步按钮设置监听回调
btn_last.onClick.AddListener(delegate () {
prenseter.StepLast();
});
// 为下一步按钮设置监听回调
btn_next.onClick.AddListener(delegate () {
prenseter.StepNext();
});

IGlassPresenter(继承IGlassPresenter):

present中UI对应处理方法改变model,处理完调用view中方法显示处理后的视图展示:

 public bool StepLast()
{
currentStep--;
if (currentStep < )
{
currentStep = ;
return false;
}
onStepChange(, currentStep);
return true;
}
// 下一步逻辑
public bool StepNext()
{ if (steps.Count <= )
{
return false;
}
currentStep++;
if (currentStep >= steps.Count)
{
currentStep = steps.Count-;
return false; }
onStepChange(,currentStep);
return true;
}
// 步骤改变调用
public void onStepChange(int currentTaskId, int currentStepId)
{
this.currentStep = currentStepId;
LogUtil.i("currentStepId"+currentStepId);
currentStepContent =steps[currentStep].content;
view.setStepContent(currentStepContent);
view.setCurrentStep(currentStep);
}

同时present中还有加载文件的方法:

文件格式是这样子的:1 1.风险可能导致的后果:倒塌、高处坠落、公路中断运行、跨越架封网脱落等 image1

上面的FileUtil能解析出来并赋值给step(model)对象

view中显示方法(参数为model中的信息):

除了显示model信息之外还会加载其他的UI或者模型

 public void setStepContent(string content)
{
Debug.Log("content:"+content);
// 按钮文本设置为步骤内容
text.text = content;
}
  public void setCurrentStep(int currentStep)
{
currentStep = currentStep + ;
if (Application.loadedLevelName.Equals("KYJ"))
{
if (currentModel != null)
Destroy(currentModel);
Play("kyj_"+ currentStep);
LoadPrefab("Prefabs/Kyj_Step/kyj_" + currentStep, objectTarget.transform);
if (currentStep == )
endStep.SetActive(true);
else
endStep.SetActive(false);
}
}

unity 使用MVC模式的更多相关文章

  1. Unity之MVC 模式

    MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式.这种模式用于应用程序的分层开发. Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO.它 ...

  2. MVC模式下unity配置,报错“No connection string named '**Context' could be found in the application config file”

     写在前面: 第一次配置时好好的,后来第二次改到MVC模式,把依赖注入写成字典的单例模式时,由于新建的ORM(数据库映射模型EF),怎么弄都不用,一直报错"No connection str ...

  3. 【Unity】基于MVC模式的背包系统 UGUI实现

    前言 本文基于MVC模式,用UGUI初步实现了背包系统. Control层包括了点击和拖拽两种逻辑. 博文首发:http://blog.csdn.net/duzixi 下载地址:https://git ...

  4. Android 腾讯入门教程( 智能手表UI设计 和 MVC模式 )

    *****注意到mvc 在android 中是如何进行分层分域执行各自的功能.**** 官方推荐的按钮尺寸是48像素 前端之Android入门(1):环境配置 前端之Android入门(2):程序目录 ...

  5. ASP.Net MVC开发基础学习笔记:一、走向MVC模式

    一.ASP.Net的两种开发模式 1.1 ASP.Net WebForm的开发模式 (1)处理流程 在传统的WebForm模式下,我们请求一个例如http://www.aspnetmvc.com/bl ...

  6. [ASP.NET MVC 小牛之路]01 - 理解MVC模式

    本人博客已转移至:http://www.exblr.com/liam  PS:MVC出来很久了,工作上一直没机会用.出于兴趣,工作之余我将展开对MVC的深入学习,通过博文来记录所学所得,并希望能得到各 ...

  7. 深入理解MVC模式

    一,什么是MVC模式 该模式是一种软件设计典范,他把软件系统划分为三个基本部分:模型层(Model).视图层(View).控制器(Controller) *Model(模型)表示应用程序核心(比如数据 ...

  8. MVC模式

                             1.MVC的概念 1.1什么是MVC? MVC是一种架构型模式,它本身不引入新的功能,只是指导我们把web应用结构做的更加合理,实现逻辑与页面相分离. ...

  9. MVC模式与Android

    MVC模式是软件工程中的一种软件架构,“Model-View-Controller”的缩写,中文翻译为“模型-视图-控制器”. MVC模式将一个交互式应用程序分为3各组件: 1.Model(模型):业 ...

随机推荐

  1. color depth 色彩深度 像素深度

    Screen.colorDepth - Web APIs | MDN https://developer.mozilla.org/en-US/docs/Web/API/Screen/colorDept ...

  2. iOS核心动画详解(CABasicAnimation)

    前言 上一篇已经介绍了核心动画在UI渲染中的位置和基本概念,但是没有具体介绍CAAnimation子类的用法,本文将介绍CABasicAnimation及其子类CASpringAnimation的用法 ...

  3. Druid对数据库密码加密的坑

    背景: 在对已有项目搭建本地环境,修改了本地ip端口和数据库帐号密码(使用了明文). 然后项目一直跑不起来,还抛出各种异常,经过分析发现主要错在这里:druid java.lang.IllegalAr ...

  4. 上手Keras

    Keras的核心数据是“模型”,模型是一种组织网络层的方式.Keras中主要的模型是Sequential模型,Sequential是一系列网络层按顺序构成的栈. Sequential模型如下: fro ...

  5. 吴超老师课程--Sqoop的安装和介绍

    SQOOP是用于对数据进行导入导出的.    (1)把MySQL.Oracle等数据库中的数据导入到HDFS.Hive.HBase中    (2)把HDFS.Hive.HBase中的数据导出到MySQ ...

  6. Nodejs学习计划

    此文章已经发表于本人博客. 由于公司要求这段时间在学习Nodejs,基本靠自摸一路走来踩了很多坑浪费很多时间,今天就来这里说下,顺便计划一下接下来的学习计划,目前自己做个博客,项目过程中学习了js类以 ...

  7. Getting Started with Rails (1)

    按照官网http://guides.rubyonrails.org/getting_started.html上学习了一下例子.在过程中有很多刚开始没理解的地方,写下来. 首先,建立了一个resourc ...

  8. IOS UIApplicationMain函数

    对于UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString ...

  9. 理解display中的box-flex属性

    今天有个同学在面试的时候碰到了使用css2和css3实现一种页面布局,要求页面效果如下: 在实现这种页面布局时,他使用了display:box-flex,下面是相应的代码: css2 方式 <! ...

  10. [转]HBase hbck——检察HBase集群的一致性

    Hbase提供了hbck命令来检查各种不一致问题.hbck的名字仿效了HDFS的fsck命令,后者是一个用于检查HDFS中不一致问题的工具.下面这段非常易懂的介绍出自于hbck的源程序. 检查数据在M ...