Skip to content

ManeSingleton

Ilia Petrov-Komotskii edited this page Sep 24, 2026 · 4 revisions

ManeSingleton

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.

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)

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