2025-04-16 21:15:15 +08:00

25 lines
621 B
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}