2025-01-02 12:15:45 +08:00

48 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/********************************************************************************
*Create By CG
*Function unityMono类单例模式
*********************************************************************************/
namespace CG.Framework
{
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance = null;
public static bool IsExisted { get; private set; } = false;//退出时做判断是否存在,防止基类报错
protected MonoSingleton() { }
public static T Instance
{
get
{
return _instance;
}
}
private void Awake()
{
if (_instance == null)
{
_instance = gameObject.GetComponent<T>();
DontDestroyOnLoad(gameObject);//切换场景不销毁
IsExisted = true;
AwakeSelf();
}
else
{
Destroy(gameObject);
}
}
protected virtual void AwakeSelf()
{
}
protected virtual void OnDestroy()
{
IsExisted = false;
}
}
}