「Unity/Csharp/シングルトン」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==MonoBehaviourを使ったシングルトン== using System.Collections; using System.Collections.Generic; using UnityEngine; public class KomaManager : MonoBehaviou...」) |
(→MonoBehaviourを使ったシングルトン) |
||
| (同じ利用者による、間の10版が非表示) | |||
| 行1: | 行1: | ||
| + | ==通常シングルトン== | ||
| + | public class SampleManager { | ||
| + | private static SampleManager mInstance; | ||
| + | private SampleManager() { | ||
| + | } | ||
| + | public static SampleManager Instance { | ||
| + | get { | ||
| + | if (mInstance == null) mInstance = new SampleManager(); | ||
| + | return mInstance; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | 通常シングルトンでMonoBehaviourを継承しているとプロパティが設定取得できなくなる | ||
| + | |||
==MonoBehaviourを使ったシングルトン== | ==MonoBehaviourを使ったシングルトン== | ||
using System.Collections; | using System.Collections; | ||
using System.Collections.Generic; | using System.Collections.Generic; | ||
using UnityEngine; | using UnityEngine; | ||
| − | public class | + | public class SampleManager : MonoBehaviour { |
| − | private static | + | private static SampleManager mInstance; |
public int x = 1; | public int x = 1; | ||
| − | private | + | private SampleManager () { |
} | } | ||
| − | public static | + | public static SampleManager Instance { |
get { | get { | ||
| − | if( mInstance == null ) { | + | if (mInstance == null) { |
| − | GameObject go = new GameObject(" | + | GameObject go = new GameObject("SampleManager"); |
| − | mInstance = go.AddComponent< | + | mInstance = go.AddComponent<SampleManager>(); |
} | } | ||
return mInstance; | return mInstance; | ||
| 行22: | 行36: | ||
} | } | ||
} | } | ||
| + | |||
| + | 使用方法 | ||
| + | SampleManager manager = SampleManager.Instance; | ||
| + | Debug.Log("x=" + manager.x); | ||
| + | |||
| + | シーン遷移しても使いたい場合 | ||
| + | <pre> | ||
| + | void Start () { | ||
| + | DontDestroyOnLoad(this); | ||
| + | } | ||
| + | </pre> | ||
| + | もしくは、クラス内に、DontDestroyOnLoad(go);を追加 | ||
| + | <pre> | ||
| + | if (mInstance == null) { | ||
| + | GameObject go = new GameObject("SampleManager"); | ||
| + | mInstance = go.AddComponent<SampleManager>(); | ||
| + | + DontDestroyOnLoad(go); | ||
| + | } | ||
| + | return mInstance; | ||
| + | </pre> | ||
==参考== | ==参考== | ||
https://qiita.com/calmbooks/items/9cf32c6dd36b724b155e | https://qiita.com/calmbooks/items/9cf32c6dd36b724b155e | ||
2022年12月7日 (水) 17:33時点における最新版
通常シングルトン
public class SampleManager {
private static SampleManager mInstance;
private SampleManager() {
}
public static SampleManager Instance {
get {
if (mInstance == null) mInstance = new SampleManager();
return mInstance;
}
}
}
通常シングルトンでMonoBehaviourを継承しているとプロパティが設定取得できなくなる
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);
シーン遷移しても使いたい場合
void Start () {
DontDestroyOnLoad(this);
}
もしくは、クラス内に、DontDestroyOnLoad(go);を追加
if (mInstance == null) {
GameObject go = new GameObject("SampleManager");
mInstance = go.AddComponent<SampleManager>();
+ DontDestroyOnLoad(go);
}
return mInstance;
