using UnityEngine;
using System.Collections;
public abstract class MonoSingletonDontDestroy<T> : MonoBehaviour where T : MonoSingletonDontDestroy<T>
{
static T instance = null;
public static T Instance
{
get
{
if (instance == null)
{
Debug.LogWarning("No Instance of " + typeof(T).ToString() + ", one is created.");
instance = new GameObject(typeof(T).ToString()).AddComponent<T>();
}
return instance;
}
}
void Awake()
{
if (instance == null)
{
instance = this as T;
instance.Init();
DontDestroyOnLoad(this);
}
else
{
Destroy(gameObject);
}
}
public virtual void Init() { }
public virtual void Quit() { }
void OnApplicationQuit()
{
if (instance != null)
{
instance.Quit();
instance = null;
}
}
}
联系客服