这两天看了下老大的项目,他基本都是用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. php 使用imagettftext()函数出问题的原因

    <?php header('Content-type: image/png'); $im = imagecreatetruecolor(400, 300); //创建画布 $white = im ...

  2. js浏览器调试

    JS调试 sources界面(主要用来控制执行) 打断点,右上角四个按钮分别是:跳到下一个断点,单步调试,跳入,跳出. 鼠标悬浮在变量上可以查看变量的属性: console界面(主要用于查看输出) 主 ...

  3. selenium入门基础知识

    内容转载自:http://blog.csdn.net/huangbowen521/article/details/7816538 1.selenium介绍: Selenium是一个浏览器自动化操作框架 ...

  4. Redis几个认识误区(转)

    add by zhj: 文章很老了,2010年的,注意,下面几点是作者认为的误区 原文:http://timyang.net/data/redis-misunderstanding/ 前几天微博发生了 ...

  5. 多线程threading.local的作用及原理?

    1.示例代码 import time import threading v = threading.local() def func(arg): # 内部会为当前线程创建一个空间用于存储:phone= ...

  6. python web框架 Django的APP以及目录介绍 django 1.11版本

    如果有很多业务请求函数 应该放在app目录 很多业务放在主站上 当用户一点跳到分站 例如 一个项目叫运维平台  他的业务 有资产管理 私有云 监控 不同业务线 chouti项目 - chouti - ...

  7. Linux学习笔记—Linux磁盘与文件系统管理(转载)

    认识EXT2文件系统 文件的系统特性 Linux的正规文件系统为Ext2 文件数据除了文件实际内容外,还包括其他属性(文件权限.文件属性). 文件系统将这两部分数据分别存放在不同的块,权限和属性放在i ...

  8. Dora.Interception, 为.NET Core度身打造的AOP框架[3]:Interceptor的注册

    在<不一样的Interceptor>中我们着重介绍了Dora.Interception中最为核心的对象Interceptor,以及定义Interceptor类型的一些约定.由于Interc ...

  9. 查询前几条记录 top limit

    SQL Server 数据库中的Top关键字可实现查询数据库表中的前几条数据,但是需要注意的是,Top关键字只能在SQL Server数据库中可以使用,而在MySQL数据库中就要使用具有同样功能的LI ...

  10. cdoj1329卿学姐与魔法

    地址:http://acm.uestc.edu.cn/#/problem/show/1329 题目: 卿学姐与魔法 Time Limit: 1200/800MS (Java/Others)     M ...