25 lines
621 B
C#
25 lines
621 B
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
//继承这种自动创建的单例模式基类,不需要我们手动去拖拽或者api去加
|
||
public class SingletonAutoMono<T> : MonoBehaviour where T:MonoBehaviour
|
||
{
|
||
private static T instance;
|
||
|
||
public static T GetInstance()
|
||
{
|
||
if (instance == null)
|
||
{
|
||
GameObject obj = new GameObject();
|
||
//设置对象名字为脚本名
|
||
obj.name = typeof(T).ToString();
|
||
//让单例不会随场景移除
|
||
DontDestroyOnLoad(obj);
|
||
instance = obj.AddComponent<T>();
|
||
}
|
||
return instance;
|
||
}
|
||
|
||
}
|