最近看了 PureMVC for UGUI 的实现,感觉有点过度设计了。Unity 基于组件模式的风格,没有层次关系管理或者约定的话,组件之间复杂的相互依赖会是噩梦,但这个问题 PureMVC 并没有解决,而且还多出来N多冗余脚本。
自己实现了一个 MVC Pattern,放到github上了,还做了一个 Editor 工具来生成模板脚本。为了与国际接轨,特地做了英文的 readme.md 括弧允悲,赏脸给个 star 吧
UMVC
=== MVC Pattern Framework for Unity3d GUI System
Model
- Holds no view data nor view state data.
- Is accessed by the
Controller
and otherModels
only - Will trigger events to notify external system of changes.
View
- UGUI Prefabs
Presenter
- Each Presenter corresponds to a View
- Holds references to elements needed for drawing
- Receive User Input
- Notify
Controller
when an user input - This script is a UI refresh operation function set
Controller
- Controls view flow.
- Holds the application state needed for that view
- Will trigger events to notify external system of changes.
- Handles events either triggered by the player in the
View
or triggered by theModels
- Each Controller corresponds to a Presenter and holds the reference of it’s
Presenter
- Holds the references of the small
Controllers
under this controller
NotificationCenter
- A notification dispatch mechanism that enables the broadcast of information to registered observers.
- Add observer in
Controllers
- Post notifications in
Models
void Start()
{
NotificationCenter.DefaultCenter.AddObserver(this, "UserDataChanged", UserDataChanged);
}
void OnDestroy()
{
NotificationCenter.DefaultCenter.RemoveObserver(this, "UserDataChanged");
}
Create Controller and Presenter from Template
- Click menu
Template Scripts
andCreate
- Type class name and namespace
- Then it will create two scripts, one
Controller
and onePresenter
–EOF–