Files
Ultimate-Fishing-Simulator-…/Assets/Scripts/Assembly-CSharp/UFS3/Management/SceneSingleton.cs
2026-03-04 09:37:33 +08:00

47 lines
834 B
C#

using UnityEngine;
namespace UFS3.Management
{
public class SceneSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
private static readonly object _lock = new object();
public static T Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = Object.FindObjectOfType<T>();
if (_instance == null)
{
_instance = new GameObject(typeof(T).Name).AddComponent<T>();
}
}
}
}
return _instance;
}
}
protected virtual void Awake()
{
if (_instance != null && _instance != this)
{
Debug.Log("Copy of Singleton Instance already exists, destroying new one.");
Object.Destroy(base.gameObject);
}
else
{
_instance = this as T;
}
}
}
}