using UnityEngine; public class SingletonMonoBehaviour : MonoBehaviour where T : SingletonMonoBehaviour { protected static T instance; /// /// インスタンスの取得 /// public static T Instance { get { if (instance == null) { instance = (T)FindObjectOfType (typeof(T)); if (instance == null) { Debug.LogWarning (typeof(T) + "is nothing"); } } return instance; } } /// /// シーン開始時にインスタンスの生成をおこなう /// protected void Awake() { CheckInstance(); } /// /// インスタンスが存在するかのチェック /// /// なければ新たに生成 protected bool CheckInstance() { if( instance == null) { instance = (T)this; return true; } else if( Instance == this ) { /// シーンを通して存在できるように DontDestroyOnLoad (this.gameObject); return true; } Destroy(this); return false; } }