Repository navigation
ManeSingleton
Ilia Petrov-Komotskii edited this page Sep 24, 2026
·
4 revisions
ManeSingleton<T> is a thread-safe singleton base. T must inherit ManeSingleton<T>.
The first access to Instance creates T through its parameterless constructor (public or non-public). Call SetInstance first if there is no parameterless constructor.
using Mane.DotNet;
public sealed class GameConfig : ManeSingleton<GameConfig>
{
public int MaxPlayers { get; set; } = 4;
private GameConfig() { }
}
int cap = GameConfig.Instance.MaxPlayers;| Member | Behavior |
|---|---|
Instance |
Returns the stored instance, or creates one. Double-checked lock. |
IsReady() |
True if an instance exists, without creating one. |
SetInstance(T) |
Injects an instance. Null throws. Replaces whatever was stored. |
ClearInstance() |
Drops the stored reference. Does not dispose anything. |
A protected constructor registers this. Constructing a second object while another instance is already stored throws InvalidOperationException.
if (!AudioCatalog.IsReady())
AudioCatalog.SetInstance(new AudioCatalog(loadedTable));
AudioCatalog.ClearInstance(); // next Instance access creates a new one (if a ctor exists)- Missing parameterless constructor and no
SetInstance→InvalidOperationExceptionon firstInstanceaccess. -
SetInstancecan replace a live instance without callingClearInstance. - This is not a Unity component. For scene objects use
UnitySingleton<T>in ManeTools-Unity.
openupm add com.manefunction.tools-dotnet