using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class monosingleton<T> : MonoBehaviour//定义为抽象就是不能被实例化 ,只有继承了他 才能被使用,这样比较安全
where T:MonoBehaviour
{
private static T instance;
public static T Instance
{
get
{
return instance;
}
}
protected virtual void Awake()//继承这个脚本时会调用这个方法 因为是保护的虚方法 子类继承时会自动调用
{
instance = this as T;
}
}
另一种写法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class monosingleton<T> where T : new() //定义为抽象就是不能被实例化 ,只有继承了他 才能被使用,这样比较安全
{
private static T instance;
public static T Instance
{
get
{
return instance;
// if (instance == null)//这里也可以这样赋值 就不用在awake方法里面写
// instance = new T();
// return instance;
}
}
protected virtual void Awake()//继承这个脚本时会调用这个方法 因为是保护的虚方法 子类继承时会自动调用
{
instance = new T();
}
}