←All patterns
Architectural

MVP

Split UI code into a dumb View, a pure-data Model and a Presenter that mediates between them.

What Is It?

Model–View–Presenter divides a screen into three roles. The Model holds state and rules (gold, items, "can I afford this?") as plain C#. The View is a MonoBehaviour that owns the Buttons and TMP_Texts and nothing else. The Presenter sits in between and is the only piece that talks to both.

Traffic flows in a loop. A click in the View raises an event like BuyClicked; the Presenter turns that into model.TryBuy(potion); the Model changes and raises Changed; the Presenter reads the new state and calls view.Render(gold, items, canBuy). The View never holds a reference to the Model.

Think of a shop screen: the shopkeeper prices, the player wallet and the inventory rules live in an InventoryModel you can unit test without a scene, while the InventoryView could be swapped from uGUI to UI Toolkit without touching a single game rule.

When Is It Used?

Use it for any UI with real logic behind it — shops, inventories, crafting screens, settings menus with validation.

It pays off when you want automated tests for UI rules, or when designers frequently rework layouts and you do not want those changes to break gameplay code.

For a static pause menu with three buttons that just call SceneManager.LoadScene, MVP is ceremony. Keep it simple until state and rules appear.

Interactive Demo

Click the controls and watch the objects collaborate. The console mirrors what the C# code below would log.

MVP Pattern
model.Gold50items0/6Render()0
✕ no reference
InventoryView
MonoBehaviour · UI only
Presenter
plain C#
InventoryModel
plain C# · events
Gold50g
⇄
Gold50
Items0/6
event Action Changed
Console
Press a button to see what happens…

Code

Assets / Scripts / Architectural/ MVP ›InventoryBootstrap.cs
using UnityEngine;

namespace Patterns.Architectural.MVP
{
    /// <summary>
    /// Builds the triad for the shop screen: creates the Model, finds the
    /// View in the scene and hands both to a new Presenter.
    /// </summary>
    public class InventoryBootstrap : MonoBehaviour
    {
        [SerializeField] private InventoryView view;
        [SerializeField] private int startingGold = 50;

        private InventoryPresenter presenter;

        private void Start()
        {
            var model = new InventoryModel(startingGold);
            presenter = new InventoryPresenter(model, view);
        }

        private void OnDestroy()
        {
            presenter?.Dispose();
            presenter = null;
        }
    }
}
4 files · namespace Patterns.Architectural.MVPC# · UTF-8 · LF

Before & After

Real-world smell: a shop screen that owns its own gold. Here is the code without the pattern, the refactored version, and the exact diff between them.

! What’s wrong with it?

InventoryView holds the gold, the item list, the potion price and the bag capacity, and its OnBuy both checks the rules and writes straight into the TMP_Text labels.

To test "can I afford a potion?" you need a scene, a Canvas and five wired references. The same affordability rule appears in both OnBuy and Render, and moving the screen to UI Toolkit would mean rewriting the shop logic along with the layout.

✓ What changed

  • Gold, items, Capacity and the buy rule move into InventoryModel, plain C# that raises Changed; startingGold moves to InventoryBootstrap.
  • Prices and click handling move into InventoryPresenter, which turns BuyClicked into model.TryBuy and calls view.Render after every change.
  • The view keeps only its UI fields: OnBuy and OnAddGold just raise events, and Render(gold, items, canBuy) draws whatever it is handed.
  • The view no longer knows any price or capacity, so moving the screen to UI Toolkit means writing a new view and nothing else.
InventoryView.Before.cs → InventoryView.After.cs+11/−32
1+using System;
12 using System.Collections.Generic;
23 using TMPro;
34 using UnityEngine;
45 using UnityEngine.UI;
56
67 namespace Patterns.Architectural.MVP
78 {
89 /// <summary>
9− /// Without MVP: one MonoBehaviour owns the gold, the bag, the shop
10− /// rules and every label. None of it can be tested without a scene.
10+ /// With MVP: the view only owns UI references, forwards clicks as
11+ /// events and draws whatever the Presenter tells it to.
1112 /// </summary>
1213 public class InventoryView : MonoBehaviour
1314 {
1415 [SerializeField] private Button buyButton;
1516 [SerializeField] private Button addGoldButton;
1617 [SerializeField] private TMP_Text goldLabel;
1718 [SerializeField] private TMP_Text itemsLabel;
1819 [SerializeField] private TMP_Text feedbackLabel;
19− [SerializeField] private int startingGold = 50;
2020
21− private const string PotionId = "Potion";
22− private const int PotionPrice = 30;
23− private const int GoldPerClick = 25;
24− private const int Capacity = 6;
21+ public event Action BuyClicked;
22+ public event Action AddGoldClicked;
2523
26− private readonly List<string> items = new();
27− private int gold;
28−
29− private void Awake() => gold = startingGold;
30−
3124 private void OnEnable()
3225 {
3326 buyButton.onClick.AddListener(OnBuy);
3427 addGoldButton.onClick.AddListener(OnAddGold);
35− Render();
3628 }
3729
3830 private void OnDisable()
3931 {
4032 buyButton.onClick.RemoveListener(OnBuy);
4133 addGoldButton.onClick.RemoveListener(OnAddGold);
4234 }
4335
44− private void OnBuy()
45− {
46− if (gold < PotionPrice || items.Count >= Capacity)
47− {
48− feedbackLabel.text = "Not enough gold or bag is full.";
49− return;
50− }
36+ private void OnBuy() => BuyClicked?.Invoke();
5137
52− gold -= PotionPrice;
53− items.Add(PotionId);
54− Render();
55− }
38+ private void OnAddGold() => AddGoldClicked?.Invoke();
5639
57− private void OnAddGold()
40+ public void Render(int gold, IReadOnlyList<string> items, bool canBuy)
5841 {
59− gold += GoldPerClick;
60− Render();
61− }
62−
63− private void Render()
64− {
6542 goldLabel.text = $"{gold} g";
6643 itemsLabel.text = items.Count == 0 ? "(empty)" : string.Join(", ", items);
67− buyButton.interactable = gold >= PotionPrice && items.Count < Capacity;
44+ buyButton.interactable = canBuy;
6845 }
46+
47+ public void ShowFeedback(string message) => feedbackLabel.text = message;
6948 }
7049 }

Advantages & Disadvantages

+ Advantages

  • Game rules live in a plain C# Model that can be tested in Edit Mode.
  • Views become thin and replaceable — swap uGUI for UI Toolkit by writing a new View.
  • The data flow is explicit and one-directional: click → presenter → model → event → presenter → view.

− Disadvantages

  • More classes and more wiring per screen than a single MonoBehaviour would need.
  • Presenters can bloat into "god objects" if every screen concern is dumped into them.
  • Forgetting to unsubscribe in Dispose leaves presenters alive after their screen is gone.

Tips

  1. 01Give the View an interface (IInventoryView) so the Presenter can be tested with a fake view.
  2. 02Have the View expose C# events (event Action BuyClicked) rather than public Button fields, so the Presenter never touches Unity UI types.
  3. 03Make the Presenter IDisposable and unhook all events in Dispose(); call it from the bootstrap in OnDestroy.
  4. 04Render from the Model's current state on every change instead of patching individual labels — it keeps the View consistent.
  5. 05MVVM with UI Toolkit data binding is a close cousin; pick MVP when you want explicit control over each refresh.