Mvvm binding system

From nlelouche/Unity-SkillForge · View interactive version

Mvvm binding system is an AI agent skill from the GitHub repository nlelouche/Unity-SkillForge. Implements a full Model-View-ViewModel (MVVM) architecture for Unity UI Toolkit. Features reactive data binding, INotifyPropertyChanged integration, and the [CreateProperty] source generator workflow.

How to use this skill

Skill file

MVVM Binding System (UI Toolkit)

Overview

MVVM separates game data (Model) from display logic (ViewModel) from rendering (View). In Unity 6, UI Toolkit's native data binding system makes this first-class: bind a C# property directly to a UXML element attribute with zero boilerplate. This skill generates the complete M-V-VM triad: the data Model, the observable ViewModel, and the UXML/USS View.

When to Use

Architecture

┌───────────────────────────────────────────────────────────┐
│                    MVVM TRIAD                             │
├───────────────────────────────────────────────────────────┤
│                                                           │
│  MODEL                  VIEWMODEL             VIEW        │
│  ┌───────────────┐      ┌──────────────────┐  ┌───────┐  │
│  │ PlayerModel   │      │HealthBarViewModel│  │ UXML  │  │
│  │               │      │                  │  │       │  │
│  │ float Health  │─────▶│[CreateProperty]  │◀─│bind:  │  │
│  │ float MaxHealth│     │ float Health     │  │health │  │
│  │               │      │ float FillRatio  │  │-value │  │
│  │ (Plain POCO)  │      │ (Observable)     │  └───────┘  │
│  └───────────────┘      └──────────────────┘             │
│                                │                         │
│                       INotifyBindablePropertyChanged      │
│                       (Unity 6 runtime binding)          │
└───────────────────────────────────────────────────────────┘

MVVM Layer Contracts

| Layer | Class Type | Responsibility |

|-------|-----------|----------------|

| Model | Plain C# class / ScriptableObject | Holds raw game data. No Unity UI references. |

| ViewModel | Plain C# class implementing INotifyBindablePropertyChanged | Transforms Model data into bindable properties. Contains display logic. |

| View | UXML + USS + UIDocument component | Renders properties. Zero game logic. |

TDD Contract

// Test the ViewModel independently — no Unity editor required
[Test]
public void HealthBarViewModel_WhenHealthChanges_FillRatioUpdates()
{
    var model = new PlayerModel { Health = 50f, MaxHealth = 100f };
    var vm = new HealthBarViewModel(model);

    model.Health = 25f;
    vm.RefreshFromModel(); // Explicit refresh (or use events)

    Assert.AreApproximatelyEqual(0.25f, vm.FillRatio, 0.001f);
}

[Test]
public void HealthBarViewModel_WhenHealthZero_FillRatioIsZero()
{
    var model = new PlayerModel { Health = 0f, MaxHealth = 100f };
    var vm = new HealthBarViewModel(model);
    Assert.AreApproximatelyEqual(0f, vm.FillRatio, 0.001f);
}

Procedure

  1. Define the Model (plain POCO — no Unity API):

```csharp

public class PlayerModel

{

public float Health { get; set; }

public float MaxHealth { get; set; }

}

```

  1. Create the ViewModel (implements INotifyBindablePropertyChanged):

```csharp

using Unity.Properties;

using UnityEngine.UIElements;

public partial class HealthBarViewModel : INotifyBindablePropertyChanged

{

public event EventHandler<BindablePropertyChangedEventArgs> propertyChanged;

private readonly PlayerModel _model;

private float _fillRatio;

[CreateProperty]

public float FillRatio

{

get => _fillRatio;

private set

{

if (Mathf.Approximately(_fillRatio, value)) return;

_fillRatio = value;

Notify(nameof(FillRatio));

}

}

public HealthBarViewModel(PlayerModel model)

{

_model = model;

RefreshFromModel();

}

public void RefreshFromModel()

{

FillRatio = _model.MaxHealth > 0f ? _model.Health / _model.MaxHealth : 0f;

}

private void Notify(string property) =>

propertyChanged?.Invoke(this, new BindablePropertyChangedEventArgs(property));

}

```

  1. Create the View (UXML):

```xml

<!-- HealthBarView.uxml -->

<ui:UXML xmlns:ui="UnityEngine.UIElements">

<ui:VisualElement class="health-bar-container">

<ui:ProgressBar name="health-bar"

binding-path="FillRatio"

low-value="0"

high-value="1" />

<ui:Label name="health-label" binding-path="HealthText" />

</ui:VisualElement>

</ui:UXML>

```

  1. Wire the Binding in MonoBehaviour:

```csharp

using UnityEngine;

using UnityEngine.UIElements;

[RequireComponent(typeof(UIDocument))]

public class HealthBarView : MonoBehaviour

{

private UIDocument _document;

private HealthBarViewModel _viewModel;

// Injected or resolved via ServiceLocator

[SerializeField] private PlayerModel _model;

private void Awake()

{

_document = GetComponent<UIDocument>();

_viewModel = new HealthBarViewModel(_model);

}

private void OnEnable()

{

_document.rootVisualElement.dataSource = _viewModel;

}

private void OnDisable()

{

_document.rootVisualElement.dataSource = null;

}

}

```

Best Practices

Few-Shot Examples

Example 1: Ammo Counter HUD

User: "Create an ammo counter that updates when I shoot."

Agent: Generates AmmoModel, AmmoCounterViewModel with [CreateProperty] int CurrentAmmo, and a UXML <Label binding-path="CurrentAmmo" />. Subscribes to WeaponSystem.OnFired → updates model → ViewModel notifies → binding updates label.

Example 2: Two-Way Settings Binding

User: "Bind a toggle to the mute audio setting."

Agent: Creates AudioSettingsViewModel with [CreateProperty] bool IsMuted { get; set; } where the setter calls AudioMixer.SetFloat("Volume", value ? -80f : 0f). The UXML <Toggle binding-path="IsMuted"/> updates bidirectionally.

Performance Notes

| Approach | Update Cost | GC Alloc |

|----------|:-----------:|:---------:|

| Direct UI update in Update() | O(all_elements)/frame | High |

| Event-driven dirty flags | O(changed_only) | Low |

| MVVM native binding (Unity 6) | O(dirty_props)/frame | 0 bytes |

Related Skills

Template Files

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.