单例模版
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour
where T: MonoBehaviour//约束 T 必须继承MonoBehaviour
{
private static T m_instance;
public static T Instance
{
get
{
return m_instance;
}
}
protected virtual void Awake()
{
m_instance = this as T;//转换类型
}
}
要实现单例的类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//总对象池
public class ObjectPool : MonoSingleton<ObjectPool> {
…
}