44 lines
1.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public abstract class Singleton<T> : MonoBehaviour where T : Component
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<T>();
if (_instance == null)
{
GameObject obj = new GameObject();
obj.name = typeof(T).Name;
_instance = obj.AddComponent<T>();
}
}
return _instance;
}
}
private void Awake()
{
if (_instance == null)
{
_instance = this as T;
DontDestroyOnLoad(this);
SceneManager.sceneLoaded += OnSceneLoaded;
}
else
{
Destroy(this);
}
}
protected abstract void OnSceneLoaded(Scene scene, LoadSceneMode mode);
}