This repository has been archived on 2026-01-20. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
AW_2025/Assets/Scripts/Util/Singleton.cs
2025-02-24 15:18:12 +09:00

32 lines
973 B
C#

using UnityEngine;
public class Singleton<T> : MonoBehaviour
where T : Component
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
var objs = FindObjectsOfType(typeof(T)) as T[];
if (objs.Length > 0)
_instance = objs[0];
if (objs.Length > 1)
{
Debug.LogError("There is more than one " + typeof(T).Name + " in the scene.");
}
if (_instance == null)
{
GameObject obj = new GameObject();
obj.name = string.Format("_{0}", typeof(T).Name);
_instance = obj.AddComponent<T>();
}
}
return _instance;
}
}
}