Skip to content

ManeSingleton

Ilia Petrov-Komotskii edited this page Oct 11, 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;

Lifecycle

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.

Constructing an object does not register it. Instance stores the object only after its constructor has finished, so other threads never see a half-built instance. If the constructor throws, its exception reaches the caller and nothing is stored.

if (!GameConfig.IsReady())
    GameConfig.SetInstance(new GameConfig(loadedTable));

GameConfig.ClearInstance(); // next Instance access creates a new one (if a ctor exists)

Caveats

  • Missing parameterless constructor and no SetInstance → InvalidOperationException on first Instance access.
  • SetInstance can replace a live instance without calling ClearInstance.
  • This is not a Unity component. For scene objects use UnitySingleton<T> in ManeTools-Unity.

Clone this wiki locally