C#

[自分メモ]UnityでC#のシングルトンの書き方

シングルトンの書き方をよく忘れてしまうので自分メモ。

public class HogeManager : MonoBehaviour
{
    public static HogeManager instance;

    private void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }

        instance = this;
        //DontDestroyOnLoad(gameObject); DontDestroyOnLoad使うかは、ものによる
    }

}