上海军工路隧道单机版电子沙盘
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.
 
 
 
 

90 lines
2.4 KiB

using UnityEngine;
using System.Collections;
namespace AX.DevelopEngine
{
/// <summary>
/// 有DontDestroyOnLoad属性的单例泛型模板
/// </summary>
/// <typeparam name="T"></typeparam>
public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
private static T instance = null;
public static T Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType(typeof(T)) as T;
if (instance == null)
{
instance = new GameObject("_" + typeof(T).Name).AddComponent<T>();
}
DontDestroyOnLoad(instance);
}
return instance;
}
}
void OnApplicationQuit() { if (instance != null) instance = null; }
public static T CreateInstance()
{
if (Instance != null) Instance.OnCreate();
return Instance;
}
protected virtual void OnCreate()
{
}
public static bool InstanceNull()
{
if (instance == null) return true;
return false;
}
}
/// <summary>
/// 无DontDestroyOnLoad属性的单例泛型模板
/// </summary>
/// <typeparam name="T"></typeparam>
public class SingletonMono<T> : MonoBehaviour where T : SingletonMono<T>
{
private static T instance = null;
public static T Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType(typeof(T)) as T;//只能获取激活的物体
if (instance == null)
{
instance = new GameObject("_" + typeof(T).Name).AddComponent<T>();
//DontDestroyOnLoad(instance);
}
//if (instance == null)
// Console.LogError("Failed to create instance of " + typeof(T).FullName + ".");
}
return instance;
}
}
void OnApplicationQuit() { if (instance != null) instance = null; }
public static T CreateInstance()
{
if (Instance != null) Instance.OnCreate();
return Instance;
}
protected virtual void OnCreate()
{
}
}
}