Add NetworkBehaviour variants of Singleton classes
Current Behavior
The current singleton implementation classes (Singleton<T>, PersistentSingleton<T>, and RegulatorSingleton<T>) all inherit from MonoBehaviour, which limits their use to local Unity components. This makes them unsuitable for networked components that need to inherit from NetworkBehaviour.
Desired Behavior
Create network-aware variants of all singleton classes by implementing NetworkSingleton<T>, NetworkPersistentSingleton<T>, and NetworkRegulatorSingleton<T> that inherit from NetworkBehaviour.
Technical Details
Current implementations:
public class Singleton<T> : MonoBehaviour where T : Component
public class PersistentSingleton<T> : MonoBehaviour where T : Component
public class RegulatorSingleton<T> : MonoBehaviour where T : Component
Proposed network variants:
public class NetworkSingleton<T> : NetworkBehaviour where T : Component
public class NetworkPersistentSingleton<T> : NetworkBehaviour where T : Component
public class NetworkRegulatorSingleton<T> : NetworkBehaviour where T : Component
Use Case
This enhancement would allow the singleton patterns to be used with networked components, which is essential for managing network-aware game systems like:
- Network managers
- Player spawners
- Network state controllers
- Multiplayer game mode handlers
- Network-aware regulators and persistent systems
Additional Context
The current singleton implementations provide different levels of persistence and lifecycle management:
Singleton: Basic singleton pattern
PersistentSingleton: Adds scene persistence and auto-unparenting
RegulatorSingleton: Manages initialization timing and cleanup of older instances
These features need to be available for networked components to support multiplayer architectures with the same level of functionality.
Implementation Notes
To prevent unnecessary code inclusion for projects not using networking, the network variants should be conditionally compiled using a preprocessor directive. While MULTIPLAYER_TOOLS might seem like a natural choice, it's not ideal since networking packages like Netcode for GameObjects (NGO) can work without the Unity Multiplayer Tools package.
Instead, we should introduce a new preprocessor symbol UNITY_UTILS_NGO that users can define to enable these network-aware singletons. Example implementation:
#if UNITY_UTILS_NGO
using Unity.Netcode;
public class NetworkSingleton<T> : NetworkBehaviour where T : Component {
// Implementation
}
#endif
This approach:
- Allows users to explicitly opt-in to network functionality
- Avoids dependencies on specific Unity package preprocessor definitions
- Keeps the codebase clean for non-networked projects
- Provides flexibility for supporting different networking solutions in the future
Add NetworkBehaviour variants of Singleton classes
Current Behavior
The current singleton implementation classes (
Singleton<T>,PersistentSingleton<T>, andRegulatorSingleton<T>) all inherit fromMonoBehaviour, which limits their use to local Unity components. This makes them unsuitable for networked components that need to inherit fromNetworkBehaviour.Desired Behavior
Create network-aware variants of all singleton classes by implementing
NetworkSingleton<T>,NetworkPersistentSingleton<T>, andNetworkRegulatorSingleton<T>that inherit fromNetworkBehaviour.Technical Details
Current implementations:
Proposed network variants:
Use Case
This enhancement would allow the singleton patterns to be used with networked components, which is essential for managing network-aware game systems like:
Additional Context
The current singleton implementations provide different levels of persistence and lifecycle management:
Singleton: Basic singleton patternPersistentSingleton: Adds scene persistence and auto-unparentingRegulatorSingleton: Manages initialization timing and cleanup of older instancesThese features need to be available for networked components to support multiplayer architectures with the same level of functionality.
Implementation Notes
To prevent unnecessary code inclusion for projects not using networking, the network variants should be conditionally compiled using a preprocessor directive. While
MULTIPLAYER_TOOLSmight seem like a natural choice, it's not ideal since networking packages like Netcode for GameObjects (NGO) can work without the Unity Multiplayer Tools package.Instead, we should introduce a new preprocessor symbol
UNITY_UTILS_NGOthat users can define to enable these network-aware singletons. Example implementation:This approach: