架构大更
This commit is contained in:
35
Assets/Scripts/SLSUtilities/General/Singleton.cs
Normal file
35
Assets/Scripts/SLSUtilities/General/Singleton.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace SLSFramework.General
|
||||
{
|
||||
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
|
||||
{
|
||||
protected static T instance;
|
||||
public static T Instance => instance;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
Initialize(false);
|
||||
}
|
||||
|
||||
protected virtual void Initialize(bool dontDestroy)
|
||||
{
|
||||
if (dontDestroy)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = this as T;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
instance = this as T;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user