Skip to content

Singletons

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

Three Unity-side singletons, plus the engine-free ManeSingleton in ManeTools-dotNET.

Type Backing When to use
ManeSingleton<T> Plain C# Domain code, no GameObject
UnitySingleton<T> MonoBehaviour Needs a scene object / Update
ScriptableSingleton<T> ScriptableObject Data asset, or a hidden runtime instance
MainThreadDispatcher UnitySingleton Queue work from background threads

UnitySingleton

First access to Instance finds a scene component (including inactive) or creates a DontDestroyOnLoad object named [TypeName]. SetInstance / ClearInstance / IsReady() match the dotNET singleton.

using Mane.Unity;
using UnityEngine;

public class AudioHub : UnitySingleton<AudioHub>
{
    public void Play(AudioClip clip) { /* ... */ }
}

AudioHub.Instance.Play(clip);

A second instance in Awake throws if another is already registered. Destroyed Unity objects count as not ready.

ScriptableSingleton

Looks for a loaded asset first, otherwise creates a hidden runtime instance (HideAndDontSave). Prefer assigning an asset and calling SetInstance (or cloning it yourself) so play mode does not mutate the project asset.

MainThreadDispatcher

MainThreadDispatcher.RunOnMainThread(() => uiText.text = result);

Must exist before you call RunOnMainThread (place it in a scene, or touch Instance on the main thread first). If it is not ready, the call logs an error and returns. Actions run on the next Update.

Clone this wiki locally