com.xmobitea.changx.mini-singleton
v1.5.2
Published
Scene-bound singleton base and optional persistence helper for Unity MonoBehaviour managers.
Readme
XmobiTea Singleton
Scene-authored singleton utilities for Unity MonoBehaviour managers.
This package gives you:
Singleton<T>for one cached scene instance per manager typeDontDestroyfor optional cross-scene persistence on the same hostGameObjectOnInit()for one-time setup on the surviving instance
It does not auto-create missing objects, and instance can return null.
AI Entry Points
Open the smallest file for the task:
| Need | Use |
| --- | --- |
| Generate common runtime code | AI_USAGE.md |
| Scene setup and lifecycle requirements | AI_SETUP.md |
| Exact signatures and package metadata | AI_API_REFERENCE.md |
| Lookup, cache, duplicate, and persistence timing | AI_BEHAVIOR.md |
| Agent guardrails and maintenance checklist | AGENTS.md |
| Missing or conflicting docs | Runtime/Singleton.cs, Runtime/DontDestroy.cs |
Runtime Contract
Singleton<T>only works with components that already exist on loaded sceneGameObjects.instancereturns the cached instance when available.- If the cache is empty,
instanceuses the defaultFindObjectOfType<T>()lookup, which does not include inactiveGameObjects. - If no matching component is found,
instancereturnsnull. isExistsonly checks whether the private static cache currently evaluates as assigned.- Duplicate singleton hosts destroy their own
GameObjectinAwake(). DontDestroycallsGameObject.DontDestroyOnLoad(gameObject)inStart().
Setup
- Create a component that derives from
Singleton<T>. - Attach it to an active
GameObjectin a loaded scene. - Access
instanceonly after the host is expected to exist. - Add
DontDestroyonly when the same host must survive scene loads. - Avoid multiple active hosts of the same singleton type across loaded scenes.
See AI_SETUP.md for detailed setup and lifecycle notes.
Usage
using UnityEngine;
using XmobiTea.MiniSingleton;
public sealed class GameManager : Singleton<GameManager>
{
protected override void OnInit()
{
Application.targetFrameRate = 60;
}
public void RestartLevel()
{
Debug.Log("Restart level");
}
}Safe access when scene setup is uncertain:
var manager = GameManager.instance;
if (manager == null)
{
Debug.LogError("Missing GameManager in loaded scene.");
return;
}
manager.RestartLevel();Cache-only check when you explicitly do not want a scene lookup:
if (GameManager.isExists)
{
GameManager.instance.RestartLevel();
}Lifecycle Notes
- The surviving instance runs
OnInit()once per component instance. instancemay initialize a found object before that object's ownAwake()runs if another component accesses it earlier.OnInit()is the right place for setup that other code may need beforeStart().- Avoid putting required singleton setup in a derived
Awake()method; the package-ownedAwake()handles cache assignment, duplicate destruction, and theOnInit()call. DontDestroydoes not enforce uniqueness; it only makes the host persistent afterStart().
Do And Do Not
- Do null-check
instancewhen scene setup is not guaranteed. - Do use
OnInit()for one-time singleton setup. - Do keep unrelated critical components off possible duplicate hosts.
- Do not assume
isExistsmeans a matching object exists somewhere in the scene. - Do not assume this package creates missing managers.
- Do not use a derived
Awake()method for setup that must run after singleton cache assignment. - Do not add
DontDestroywhen the manager should remain scene-local.
Common Mistakes
- Using
isExistsas a scene existence check. - Accessing
instancefrom code that can run before the host scene object exists. - Placing two active hosts of the same singleton type in concurrently loaded scenes.
- Expecting an inactive host to be found by
instancebefore Unity activates it and runsAwake(). - Expecting
DontDestroyto apply beforeStart().
Runtime Files
Runtime/Singleton.cs
Runtime/DontDestroy.csPackage Metadata
- Package:
com.xmobitea.changx.mini-singleton - Runtime assembly:
com.xmobitea.changx.mini-singleton.runtime - Namespace:
XmobiTea.MiniSingleton - Minimum Unity version declared:
2022.3 - Declared dependency:
com.xmobitea.changx.app1.5.2 - License:
Apache-2.0
