forked from zxl/LaboratoryProtection
33 lines
786 B
C#
33 lines
786 B
C#
using System;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
|
|
namespace UnityTest.ZXL
|
|
{
|
|
/// <summary>
|
|
/// 继承MonoBehavior的单例基类
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public abstract class BaseAutoMono<T> : SerializedMonoBehaviour where T : MonoBehaviour
|
|
{
|
|
private static T instance;
|
|
|
|
public static T Instance()
|
|
{
|
|
if (instance == null)
|
|
{
|
|
GameObject go = new GameObject();
|
|
go.name = typeof(T).ToString();
|
|
DontDestroyOnLoad(go);
|
|
instance = go.AddComponent<T>();
|
|
}
|
|
|
|
return instance;
|
|
}
|
|
|
|
void Awake()
|
|
{
|
|
instance = this.gameObject.GetComponent<T>();
|
|
}
|
|
}
|
|
} |