Skip to content

Add NetworkBehaviour variants of Singleton classes #29

@sionzee

Description

@sionzee

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions