一.单例类模板
using UnityEngine;
///
/// 通用Mono单例模板
///
///
public abstract class MonoSingleton : MonoBehaviour where T : MonoSingleton
{
private static T ms_instance;
public static T Instance
{
get
{
if (ms_instance == null)
{
ms_instance = Instantiate();
}
return ms_instance;
}
}
protected static T Instantiate()
{
if (ms_instance != null) return ms_instance;
// 在场景中查找T类型的Mono类
ms_instance = (T)FindObjectOfType(typeof(T));
if (FindObjectsOfType(typeof(T)).Length > 1)
{
return ms_instance;
}
if (ms_instance != null) return ms_instance;
// 创建GameObject实例
var singleton = new GameObject("[Singleton]" + typeof(T).Name);
Do