using UnityEngine; public static class SingletonHelper { public static void Assign(ref T field, T instance) where T : Object { if (!field) { field = instance; return; } Debug.LogErrorFormat(instance, "Duplicate instance of singleton class {0}. Only one should exist at a time.", typeof(T).Name); } public static void Unassign(ref T field, T instance) where T : Object { if (field == instance) { field = null; } } public static T Assign(T existingInstance, T instance) where T : Object { if (!existingInstance) { return instance; } Debug.LogErrorFormat(instance, "Duplicate instance of singleton class {0}. Only one should exist at a time.", typeof(T).Name); return existingInstance; } public static T Unassign(T existingInstance, T instance) where T : Object { if (instance == existingInstance) { return null; } return existingInstance; } }