Unity MonoBehavior シングルトン
UnitySingleton.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitySingleton<T> : MonoBehaviour
where T : Component
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType(typeof(T)) as T;
if (instance == null)
{
GameObject obj = new GameObject();
instance = obj.AddComponent<T>();
}
}
return instance;
}
}
}
MonoBehaviour継承していますので簡単に利用できます。