unity 使用MVC模式
这两天看了下老大的项目,他基本都是用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模式的更多相关文章
- Unity之MVC 模式
MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式.这种模式用于应用程序的分层开发. Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO.它 ...
- MVC模式下unity配置,报错“No connection string named '**Context' could be found in the application config file”
写在前面: 第一次配置时好好的,后来第二次改到MVC模式,把依赖注入写成字典的单例模式时,由于新建的ORM(数据库映射模型EF),怎么弄都不用,一直报错"No connection str ...
- 【Unity】基于MVC模式的背包系统 UGUI实现
前言 本文基于MVC模式,用UGUI初步实现了背包系统. Control层包括了点击和拖拽两种逻辑. 博文首发:http://blog.csdn.net/duzixi 下载地址:https://git ...
- Android 腾讯入门教程( 智能手表UI设计 和 MVC模式 )
*****注意到mvc 在android 中是如何进行分层分域执行各自的功能.**** 官方推荐的按钮尺寸是48像素 前端之Android入门(1):环境配置 前端之Android入门(2):程序目录 ...
- ASP.Net MVC开发基础学习笔记:一、走向MVC模式
一.ASP.Net的两种开发模式 1.1 ASP.Net WebForm的开发模式 (1)处理流程 在传统的WebForm模式下,我们请求一个例如http://www.aspnetmvc.com/bl ...
- [ASP.NET MVC 小牛之路]01 - 理解MVC模式
本人博客已转移至:http://www.exblr.com/liam PS:MVC出来很久了,工作上一直没机会用.出于兴趣,工作之余我将展开对MVC的深入学习,通过博文来记录所学所得,并希望能得到各 ...
- 深入理解MVC模式
一,什么是MVC模式 该模式是一种软件设计典范,他把软件系统划分为三个基本部分:模型层(Model).视图层(View).控制器(Controller) *Model(模型)表示应用程序核心(比如数据 ...
- MVC模式
1.MVC的概念 1.1什么是MVC? MVC是一种架构型模式,它本身不引入新的功能,只是指导我们把web应用结构做的更加合理,实现逻辑与页面相分离. ...
- MVC模式与Android
MVC模式是软件工程中的一种软件架构,“Model-View-Controller”的缩写,中文翻译为“模型-视图-控制器”. MVC模式将一个交互式应用程序分为3各组件: 1.Model(模型):业 ...
随机推荐
- phpmail发送邮件
---恢复内容开始--- 首先.需要phpmailer的包. 地址:https://github.com/Synchro/PHPMailer 解开压缩包,将class.phpmailer.php,cl ...
- MyBatis 映射文件详解
1. MyBatis 映射文件之<select>标签 <select>用来定义查询操作; "id": 唯一标识符,需要和接口中的方法名一致; paramet ...
- Java 语言基础(一)
大多数编程语言都包括以下基本内容: 关键字 标识符 注释 常量和变量 运算符 语句 函数 数组 学习语言最重要的两点: 该语言基础的表现形式是什么 这些东西什么时候使用 关键字 在程序语言中有特殊含义 ...
- make Makefile 与 cmake CMakeLists.txt
make Makefile 与 cmake CMakeLists.txt 大家都知道,写程序大体步骤为: 1.用编辑器编写源代码,如.c文件. 2.用编译器编译代码生成目标文件,如.o. 3.用链接器 ...
- js生成二维码/html2canvas生成屏幕截图
1.需求简述 (1) 最初需求: 根据后台接口获取url,生成一个二维码,用户可以长按保存为图片.(这时的二维码只是纯黑白像素构成的二维码) 方案1: 使用jquery.qrcode.min.js插件 ...
- Remote System Upgrade With Cyclone III Devices
系统设计者会遇到较短的设计周期.标准发展和系统调度的挑战,飓风III系列支持远程系统更新,通过其固有的重编程功能和专有电路来克服以上问题.远程系统更新帮助传递系统性能增强和bug修复,避免了昂贵的召回 ...
- Winio.dll的使用
Winio.dll的使用 WinIO程序库允许在32位的Windows应用程序中直接对I/O端口和物理内存进行存取操作.通过使用一种内核模式的设备驱动器和其它几种底层编程技巧,它绕过了Windows系 ...
- 第二课客户端链接Linux系统
使用Putty客户端软件连接Linux主机 使用rpm –qa | grep ssh命令查看是否已经安装ssh服务,如下图是已经安装了ssh服务,如果未列出相关的openssh那么代表未安装这时候就需 ...
- LeetCode:简化路径【71】
LeetCode:简化路径[71] 题解参考天码营:https://www.tianmaying.com/tutorial/LC71 题目描述 给定一个文档 (Unix-style) 的完全路径,请进 ...
- Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)
1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...