Example: The “BadCore” Rule

A common architectural pattern in Unity (and software in general) is ensuring your Core business logic remains independent of the presentation layer (UI).

If your Core scripts reference UI elements (e.g., PlayerHealth.cs referencing HealthBar.cs), you cannot reuse PlayerHealth in a project without a GUI, or unit test it easily.

The Rule

We want to forbid: Core depends on UI.

Configuration

  • Source Pattern: .*Core.* (Any asset with “Core” in its name/path)
  • Target Pattern: .*UI.* (Any asset with “UI” in its name/path)
  • Type: Forbidden

Scenario

  1. You have a script Assets/Scripts/Core/PlayerHealth.cs.
  2. You have a script Assets/Scripts/UI/HealthBar.cs.
  3. In PlayerHealth.cs, you add:
    public HealthBar healthBar; // Direct dependency!
    

SaveGuard Reaction

If you try to save PlayerHealth.cs with SaveGuard enabled (Strict Mode):

  1. The engine sees PlayerHealth (matches .*Core.*).
  2. It detects dependency HealthBar (matches .*UI.*).
  3. Violation Triggered: “Core cannot depend on UI”.
  4. Result: The file is NOT saved. You must remove the reference.

The Fix

Use events or the Observer pattern.

  • PlayerHealth should invoke OnHealthChanged.
  • HealthBar should listen to PlayerHealth.
  • Now UI depends on Core (Allowed), but Core does not know about UI.

This site uses Just the Docs, a documentation theme for Jekyll.