使用 MVVM 模式和 Unity Engine 作为前端 [英] Using the MVVM pattern with Unity Engine as front end

查看:31
本文介绍了使用 MVVM 模式和 Unity Engine 作为前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划制作一些应用程序,让我可以在家中或其他地方组织任务.为此,我需要一个数据库服务器(在家中运行的虚拟服务器)、后端部分(业务逻辑)和前端(视图).在学校,我们通过 WPF 学习了 MVVM,但坦率地说,我仅在明确需要时才使用它.

I plan to make some applications that will let me organize tasks from home or from somewhere else. For this I need a database server (a virtual one running at home), a backend part (the business logic) and the front end (the view). At school we learned the MVVM via WPF, but to be frank I use it only when it is explicitely required.

Unity 有很多好处,比如

There are a lot of benefits in favour of Unity like

  • 对艺术家友好
  • 可以编译到移动、网络和桌面
  • 快速开发(至少在我的情况下)
  • 有很多我可以利用的内置功能.<​​/li>
  • Artist friendly
  • Can compile to mobile, web and desktop
  • Fast development (in my case at least)
  • Has a lot of built in features I can harness.

现在,即使我会制作这样的应用程序那么简单,有时我也可能想要扩展它、改进它或完全更改某些组件.我知道如何在 WPF 中使用 MVVM,但我对 Unity 实现只有一些模糊的想法,我希望熟练的人进行概述.

Now, even as simple as I will make an app like this, sometimes I might want to extend it, improve it or change some components entirely. I know how to use MVVM in WPF but I only have some vague ideas for the Unity implementation which I would like someone skilled to overview.

前端 (Unity) 将负责声音管理、精美动画和 UI 本身.但是UI按钮事件不能直接访问数据库,于是想到了搭建一些桥接类.举个简单的例子,假设我想删除一个提醒.在 OnButtonClicked() 方法中,我只会调用 BridgeClassPleaseDeleteThis(MyReminder) 并且在该方法中,有实际找到的后端解决方案该提醒由 id 并从数据库中删除,然后将 一切正常 标志发送回前端应用程序,以便我知道它成功了.

The front end (Unity) would take care of sound management, fancy animations and the UI itself. But UI button events cannot access directly the database, so I thought of building some bridge classes. Say I want to delete a reminder, as an easy example. In an OnButtonClicked() method I would just call a BridgeClass's PleaseDeleteThis(MyReminder) and inside that method there's the backend solution that actually finds that reminder by id and removes from the database, then sends an everything all right flag back to the front end app so I know it was successful.

我想到的是,使用这些桥接类我不必担心如果我更改数据库服务器或前端会发生什么,因为他们所需要知道的只是 通信桥.我知道这个例子过于简化了,但是关于组件完整性和 MVVM 模式,这个例子有多可行?

What I thought of is that using these bridge classes I wouldn't have to worry about what happens if I change the database server or the front end, as all they have to know is communicating with the bridge. I know the example was over simplified, but how doable would this one be regarding the component integrity and the MVVM pattern?

推荐答案

MVVM 已被 AAA 软件公司成功用于构建游戏.Shipbreakers(又名 Homeworld: Deserts of Kharak)的制作者编写了他们的代码,这样我相信 View 是一个 MonoBehavor,VM 和 M 是编写在外部和独立的 POCO 类Unity 以加快开发速度.他们可以拥有自己的视图,用于 Unity 外部的原型设计.

MVVM has been successfully used by AAA software houses to build games. The makers of Shipbreakers (aka Homeworld: Deserts of Kharak) wrote their code such that the View was a MonoBehavor I believe, VM and M were POCO classes written external to and independent to Unity so as to speed development. They can have their own Views for prototyping external to Unity.

他们还为 Unity 编写了自己的调试可视化工具和属性编辑器.

They also wrote their own debugging visualisers and property editors for Unity.

https://www.youtube.com/watch?v=q9aeNtKKXeo

MVVM 大约在 19:18 标记处,但我建议您观看整个过程因为那里还有其他一些很棒的东西.

MVVM is about at the 19:18 mark though I do recommend to watch the whole thing as there are some other great things in there.

这篇关于使用 MVVM 模式和 Unity Engine 作为前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆