31 lines
674 B
C#
31 lines
674 B
C#
using UnityEngine;
|
|
|
|
namespace Utils
|
|
{
|
|
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
|
|
{
|
|
public static T Instance { get; private set; }
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
if (dontDestroyOnLoad() && Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this as T;
|
|
|
|
if (dontDestroyOnLoad())
|
|
{
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
}
|
|
|
|
protected virtual bool dontDestroyOnLoad()
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
} |