2014/02/16(日) [n年前の日記]
#1 [unity] シングルトンについて勉強中
以下の解説ページを参考にして実験中。
_naichilab - Android iOSアプリ開発メモ: 【Unity】なんちゃらManagerクラスを作ろう(シングルトン)
_ざんないプログラマァのアプリ開発日記 Unity3D Singleton
_テラシュールウェア [Unity3D]もっと楽なシングルトンの実装
_Unity勉強 六日目(STG作成開始・シングルトン) - ぼっちプログラマのメモ
_[Unity]Generic Based Singleton for MonoBehaviours完全版(?) | ケットシーウェア
既に該当オブジェクトが存在してた場合、 Destroy() で自分を削除、しているつもりが削除できなくてハマったり。一応、こんな感じにしたら消えてくれたけど…合ってるのかな…。
SingletonMonoBehaviour.cs
GWk.cs
StartButton.cs
最初に実行されるシーンにあらかじめ配置しておく分には問題無いのだけど。途中のシーンをいきなり実行しようとすると、「そんなオブジェクトは無い」と言われてしまうので…。hoge.Instance が null を返した場合は、該当オブジェクトが無いから Prefab から発生( Instantiate(hoge); )、ということにしてしのいでいるけど、これも合ってるのかどうか…。
_naichilab - Android iOSアプリ開発メモ: 【Unity】なんちゃらManagerクラスを作ろう(シングルトン)
_ざんないプログラマァのアプリ開発日記 Unity3D Singleton
_テラシュールウェア [Unity3D]もっと楽なシングルトンの実装
_Unity勉強 六日目(STG作成開始・シングルトン) - ぼっちプログラマのメモ
_[Unity]Generic Based Singleton for MonoBehaviours完全版(?) | ケットシーウェア
既に該当オブジェクトが存在してた場合、 Destroy() で自分を削除、しているつもりが削除できなくてハマったり。一応、こんな感じにしたら消えてくれたけど…合ってるのかな…。
SingletonMonoBehaviour.cs
using UnityEngine;
// using System.Collections;
// Singletonを簡単に使う
public class SingletonMonoBehaviour<T> : MonoBehaviour where T : MonoBehaviour {
private static T instance;
public static T Instance {
get {
if (instance == null) {
System.Type type = typeof(T);
instance = FindObjectOfType(type) as T;
if (instance == null) {
Debug.LogError(typeof(T) + " is nothing");
}
}
return instance;
}
}
protected void Awake() {
if (instance == null) {
instance = this as T;
DontDestroyOnLoad(this.gameObject);
}
else {
// Debug.LogError(typeof(T) + " exist");
Destroy(this.gameObject);
}
}
}
GWk.cs
using UnityEngine;
using System.Collections;
// グローバルワーク。Singletonを使う
public class GWk : SingletonMonoBehaviour<GWk> {
public static int PlayerLife = 100;
public static int PlayerLifeMax = 100;
void Start() {
}
void Update() {
}
}
とりあえず、シーンに空の GameObject を配置して、その GameObject に GWk.cs を追加、かつ、GameObjectの名前を GWk に変更。さらに、その GameObject を Prefab化。StartButton.cs
using UnityEngine;
using System.Collections;
public class StartButton : MonoBehaviour {
public string NextSceneName = "";
public string ButtonText = "";
private bool pushEnable = false;
private bool nextScened = false;
private float timer = 0;
public GameObject gwk;
public GameObject fadeManager;
void Awake() {
}
void Start() {
pushEnable = false;
nextScened = false;
timer = 0.5f;
}
void Update() {
if (nextScened || pushEnable) return;
timer -= Time.deltaTime;
if (timer > 0) return;
if (Input.GetKeyDown(KeyCode.Space)
|| Input.GetButtonDown("Fire1")
|| Input.GetButtonDown("Jump")) {
// ボタンが押されたらフラグを立てる
pushEnable = true;
}
}
void OnGUI() {
if (nextScened) return;
int w = 200;
int h = 32;
int x = (Screen.width - w) / 2;
int y = (Screen.height - h - 32);
if (GUI.Button(new Rect(x, y, w, h), ButtonText)) pushEnable = true;
if (pushEnable) {
// ボタンが押された
if (GWk.Instance == null)
Instantiate(gwk);
if (Application.loadedLevelName == "about") {
// aboutシーンなら、ゲーム用ワークを初期化
GWk.PlayerLife = 100;
GWk.PlayerLifeMax = 100;
}
if (FadeManager.Instance == null)
Instantiate(fadeManager);
// シーン移行
// Application.LoadLevel(NextSceneName);
FadeManager.Instance.LoadLevel(NextSceneName, 0.3f);
nextScened = true;
}
}
}
最初に実行されるシーンにあらかじめ配置しておく分には問題無いのだけど。途中のシーンをいきなり実行しようとすると、「そんなオブジェクトは無い」と言われてしまうので…。hoge.Instance が null を返した場合は、該当オブジェクトが無いから Prefab から発生( Instantiate(hoge); )、ということにしてしのいでいるけど、これも合ってるのかどうか…。
[ ツッコむ ]
以上、1 日分です。