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
- You have a script
Assets/Scripts/Core/PlayerHealth.cs. - You have a script
Assets/Scripts/UI/HealthBar.cs. - In
PlayerHealth.cs, you add:public HealthBar healthBar; // Direct dependency!
SaveGuard Reaction
If you try to save PlayerHealth.cs with SaveGuard enabled (Strict Mode):
- The engine sees
PlayerHealth(matches.*Core.*). - It detects dependency
HealthBar(matches.*UI.*). - Violation Triggered: “Core cannot depend on UI”.
- Result: The file is NOT saved. You must remove the reference.
The Fix
Use events or the Observer pattern.
PlayerHealthshould invokeOnHealthChanged.HealthBarshould listen toPlayerHealth.- Now
UIdepends onCore(Allowed), butCoredoes not know aboutUI.