using UnityEngine;
public abstract class MonoSinglton<T> : MonoBehaviour
where T : MonoBehaviour
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<T>();
if (_instance == null)
{
var obj = new GameObject($"@{typeof(T).Name}");
_instance = obj.AddComponent<T>();
}
}
return _instance;
}
set { _instance = value; }
}
}
使用:
public class RaceAudioManager : MonoSinglton<RaceAudioManager>
{
//...
}