Boilerplate for Unity projects.
Uses MVC concept. For details read "Unity in action" book, chapters 9 & 12.
- Some UI action assigned on some controller.
- UI action calls mutation methods in any manager(s).
- Manager(s) broadcast event about mutation result.
- Any controller(s) can be subscribed to event and perform re-render or (and) call another method in manager(s).
Note. Manager's methods can be called from any place - trigger, controller or any other.
Example:
Let's say that we need to have game restarts counter.
- We have "Restart game" button and "Restarts count: 0" counter label.
OnClick
of this button assigned toUIController::OnRestartLevelClicked()
.UIController::OnRestartLevelClicked()
performManagers.Mission.Restart()
call.- In
Managers.Mission.Restart()
:- validate is action authorized;
- reload scene;
- increase restarts counter (
Managers.Mission.restartsCount++
); - broadcast
GameEvent.LEVEL_RESTARTED
event.
UIController
was subscribed to thisGameEvent.LEVEL_RESTARTED
event. Let's say handler isUIController::onLevelRestarted()
.- Handler
UIController::onLevelRestarted()
can set$"Restarts count: {Managers.Mission.restartsCount}"
to counter label. - Optionally handler can perform any other Manager's methods calls - update inventory, change player's health, ...
Used fragments from https://github.com/jhocking/uia-3e.
Thanks to Joseph Hocking for great book!