using System.Collections.Generic; using UnityEngine; public class Singleton : MonoBehaviour where Instance : Singleton { public bool isPersistant = true; public virtual void Awake() { if (instances == null) instances = new Dictionary(); string scene_name = gameObject.scene.name; if (isPersistant) { if (instances.Count == 0) { instances.Add("", this as Instance); DontDestroyOnLoad(gameObject); } else { DestroyObject(gameObject); } } else { if (instances.ContainsKey(scene_name) == false) instances.Add(scene_name, this as Instance); } } static Dictionary instances; /// ///name = gameObject.scene.name /// public static Instance GetInstance(string name = "") { string key = instances.ContainsKey(name) ? name : ""; return instances.ContainsKey(name) ? instances[key] : null; } }