30 lines
686 B
C#
30 lines
686 B
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
|
|
{
|
|
private static T _instance;
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance==null)
|
|
{
|
|
_instance = FindObjectOfType<T>();
|
|
if (_instance == null)
|
|
{
|
|
_instance = new GameObject().AddComponent<T>();
|
|
}
|
|
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
protected virtual void Awake()
|
|
{
|
|
_instance = this as T;
|
|
DontDestroyOnLoad(this.gameObject);
|
|
}
|
|
}
|