unity-singleton-pattern
Unity Singleton Pattern
A robust suite of Singleton variations for global systems. Adheres to the Ohm-Yura project rule: "Use Singletons ONLY for global systems (GameManager, UIManager, InputManager)."
Core Features
- Singleton: Standard management. Destroys new duplicates to keep the original.
- PersistentSingleton: Extends Singleton with Don't Destroy On Load (DDOL) for cross-scene state.
- RegulatorSingleton: Advanced inverse logic. Destroys OLD instances and keeps the NEWEST (useful for hot-swapping).
- StaticInstance: Lightweight global access without duplication enforcement.
Core Files (Max 3)
SingletonBase.cs.txt: Generic base classes for classic and persistent variants.SingletonRegulator.cs.txt: Logic for the time-stamped regulator variant.SingletonExample.cs.txt: Concrete examples for Game and UI managers.
Usage
1. Standard Manager
public class UIManager : Singleton<UIManager> { }
// Use via: UIManager.Instance.Show();
2. Persistent Manager
public class InventoryManager : PersistentSingleton<InventoryManager> { }
// Survives scene changes.
3. Regulator (System Swap)
public class MusicSystem : RegulatorSingleton<MusicSystem> { }
// Spawning a new one will automatically kill the old one.
Key Principle
Always use the most restrictive version. If it doesn't need to persist cross-scene, use Singleton<T>. If you just need a reference but don't mind duplicates, use StaticInstance<T>.
More from muharremtozan/unity-agent-skills
unity-so-prefab-manager
Manages the structured relationship between ScriptableObjects (Data) and Prefabs (Logic/Visuals) in Unity 6. Follows the 'SO-to-Mono' Bridge pattern to ensure instance independence (e.g., individual health for identical robots) while maintaining a clean, data-driven architecture. Use when: (1) Creating new unit/item types, (2) Wiring SO data to Prefab MonoBehaviours, (3) Resolving data-sharing bugs where changing one SO affects all instances.
16unity-fsm
Specialized skill for implementing a robust, extensible Finite State Machine in Unity using the State and Strategy patterns. Based on the pattern by Adam Myhre (3D Platformer). Use when creating complex AI, player controllers, or any system requiring structured state management.
11unity-code-reviewer
Professional Unity C# Code Reviewer. Detects anti-patterns, performance leaks, and enforces project-specific architecture.
10unity-assembly-management
Manage project boundaries using Assembly Definitions (.asmdef) for faster compile times and modular architecture. Based on the patterns by Adam Myhre. Enforces responsibility-based organization and handles Runtime/Editor/Tests splits.
9unity-event-bus
Advanced code-driven event bus with reflection-based bootstrapping. Provides zero-setup global messaging.
9unity-ui-data-binding
Implementation of MVVM-style Data Binding for Unity UI Toolkit using the [CreateProperty] attribute and BindableProperty wrappers.
9