You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
44 lines
1.1 KiB
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Singleton<Instance> : MonoBehaviour where Instance : Singleton<Instance>
|
|
{
|
|
public bool isPersistant = true;
|
|
|
|
public virtual void Awake()
|
|
{
|
|
if (instances == null)
|
|
instances = new Dictionary<string, Instance>();
|
|
|
|
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<string, Instance> instances;
|
|
|
|
///<summary>
|
|
///name = gameObject.scene.name
|
|
///</summary>
|
|
public static Instance GetInstance(string name = "")
|
|
{
|
|
string key = instances.ContainsKey(name) ? name : "";
|
|
return instances.ContainsKey(name) ? instances[key] : null;
|
|
}
|
|
}
|
|
|