「Unity/Csharp/シングルトン」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→MonoBehaviourを使ったシングルトン) |
(→MonoBehaviourを使ったシングルトン) |
||
| 行22: | 行22: | ||
} | } | ||
} | } | ||
| + | |||
| + | 使用方法 | ||
| + | SampleManager manager = SampleManager.Instance; | ||
| + | Debug.Log("x=" + manager.x); | ||
==参考== | ==参考== | ||
https://qiita.com/calmbooks/items/9cf32c6dd36b724b155e | https://qiita.com/calmbooks/items/9cf32c6dd36b724b155e | ||
2017年9月22日 (金) 23:55時点における版
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 () {
}
}
使用方法
SampleManager manager = SampleManager.Instance;
Debug.Log("x=" + manager.x);
