facebook twitter hatena line email

「Unity/Csharp/シングルトン」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(MonoBehaviourを使ったシングルトン)
(MonoBehaviourを使ったシングルトン)
行3: 行3:
 
  using System.Collections.Generic;
 
  using System.Collections.Generic;
 
  using UnityEngine;
 
  using UnityEngine;
  public class KomaManager : MonoBehaviour {
+
  public class SampleManager : MonoBehaviour {
  private static KomaManager mInstance;
+
  private static SampleManager mInstance;
 
  public int x = 1;
 
  public int x = 1;
  private KomaManager () {
+
  private SampleManager () {
 
  }
 
  }
  public static KomaManager Instance {
+
  public static SampleManager Instance {
 
  get {
 
  get {
 
  if (mInstance == null) {
 
  if (mInstance == null) {
  GameObject go = new GameObject("KomaManager");
+
  GameObject go = new GameObject("SampleManager");
  mInstance = go.AddComponent<KomaManager>();
+
  mInstance = go.AddComponent<SampleManager>();
 
  }
 
  }
 
  return mInstance;
 
  return mInstance;

2017年9月22日 (金) 23:52時点における版

MonoBehaviourを使ったシングルトン

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SampleManager : MonoBehaviour {
	private static SampleManager mInstance;
	public int x = 1;
	private SampleManager () {
	}
	public static SampleManager Instance {
		get {
			if (mInstance == null) {
				GameObject go = new GameObject("SampleManager");
				mInstance = go.AddComponent<SampleManager>();
			}
			return mInstance;
		}
	}
	void Start () {
	}
	void Update () {
	}
}

参考

https://qiita.com/calmbooks/items/9cf32c6dd36b724b155e