Programming-Design-Patterns.../Assets/_test/Singleton/MonoSingleton/MonoSingleton.cs

22 lines
No EOL
509 B
C#

using System;
using UnityEngine;
namespace _test.Singleton.MonoSingleton
{
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
public static T Instance { get; private set; }
protected virtual void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = (T)this;
DontDestroyOnLoad(gameObject);
}
}
}