学习记录
继承于MonoBehaviour的单例类,方便调用
/*
* FileName: SingletonM
* CreateTime: #CreateTime#
* Version: #Version#
* Description:
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 继承于MonoBehaviour的单利类
/// </summary>
/// <typeparam name="T"></typeparam>
public class SingletonM<T> : MonoBehaviour where T : MonoBehaviour
{
static T _instance = default(T);
public static T Instance
{
get
{
if (_instance == null)
{
_instance = (T)FindObjectOfType(typeof(T));
if (_instance == null)
{
GameObject go = new GameObject(typeof(T).Name);
_instance = go.AddComponent<T>();
}
}
return _instance;
}
}
protected virtual void Awake()
{
if (_instance == null)
{
_instance = this as T;
}
else
{
// Destroy(this.gameObject);
}
Init();
}
/// <summary>
/// 可用于初始化
/// </summary>
protected virtual void Init()
{
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容