Last active
August 16, 2016 05:38
-
-
Save Buravo46/f1c2c712772db09111cb to your computer and use it in GitHub Desktop.
【Unity】ジェネリックなシングルトンパターンを利用したスコア管理クラスとその利用方法。Sceneを遷移しても状態を持てる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System; | |
/*===============================================================*/ | |
/** | |
* ゲームの管理 | |
* 2014年12月6日 Buravo | |
*/ | |
public class Main : MonoBehaviour | |
{ | |
/*===============================================================*/ | |
/** | |
* @brief 開始時に一度呼ばれるメソッド. | |
*/ | |
void Start () | |
{ | |
// スコアの加算. | |
ScoreManager.Instance.AddScore(100); | |
// スコアの表示. | |
Debug.Log("Score : "+ScoreManager.Instance.Score); | |
} | |
/*===============================================================*/ | |
} | |
/*===============================================================*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
/*===============================================================*/ | |
/** | |
* スコアの管理をするクラス | |
* 2014年12月6日 Buravo | |
*/ | |
public class ScoreManager : Singleton<ScoreManager> | |
{ | |
#region メンバ変数 | |
/*===============================================================*/ | |
/** | |
* @brief スコア | |
*/ | |
private int m_score; | |
/** | |
* @brief ハイスコア | |
*/ | |
private int m_high_score; | |
/*===============================================================*/ | |
#endregion | |
#region アクセサ変数 | |
/*===============================================================*/ | |
/** | |
* @brief スコア | |
*/ | |
public int Score | |
{ | |
set | |
{ | |
m_score = value; | |
} | |
get | |
{ | |
return m_score; | |
} | |
} | |
/** | |
* @brief ハイスコア | |
*/ | |
public int HighScore | |
{ | |
get | |
{ | |
return m_high_score; | |
} | |
} | |
/*===============================================================*/ | |
#endregion | |
/*===============================================================*/ | |
/** | |
* @brief パラメーターなしのパブリックなコンストラクタ | |
*/ | |
public ScoreManager () {} | |
/*===============================================================*/ | |
/*===============================================================*/ | |
/** | |
* @brief スコアの加算処理 | |
*/ | |
public void AddScore (int t_add_score) | |
{ | |
m_score += t_add_score; | |
if (m_score > m_high_score) | |
{ | |
m_high_score = m_score; | |
} | |
} | |
/*===============================================================*/ | |
/*===============================================================*/ | |
/** | |
* @brief スコアの初期化 | |
*/ | |
public void Reset () | |
{ | |
m_score = 0; | |
} | |
/*===============================================================*/ | |
} | |
/*===============================================================*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System; | |
/*===============================================================*/ | |
/** | |
* ジェネリックなシングルトンパターン | |
* 参考URL : http://msdn.microsoft.com/ja-jp/library/ms998558.aspx | |
* このジェネリッククラスはclassとnewの型パラメーターの制約をしている | |
* 参考URL : http://msdn.microsoft.com/ja-jp/library/d5x73970.aspx | |
* 2014年12月6日 Buravo | |
*/ | |
public class Singleton<T> where T : class, new() | |
{ | |
#region メンバ変数 | |
/*===============================================================*/ | |
/** | |
* @brief インスタンス変数への代入が完了するまで、アクセスできなくなるジェネリックなインスタンス | |
*/ | |
private static volatile T m_instance; | |
/** | |
* @brief ロックするためのインスタンス | |
*/ | |
private static object m_sync_obj = new object (); | |
/*===============================================================*/ | |
#endregion | |
#region アクセサ変数 | |
/*===============================================================*/ | |
/** | |
* @brief ジェネリックなインスタンス | |
*/ | |
public static T Instance | |
{ | |
get | |
{ | |
// ダブルチェック ロッキング アプローチ. | |
if (m_instance == null) | |
{ | |
// m_sync_objインスタンスをロックし、この型そのものをロックしないことで、デッドロックの発生を回避 | |
lock (m_sync_obj) | |
{ | |
if (m_instance == null) | |
{ | |
m_instance = new T (); | |
} | |
} | |
} | |
return m_instance; | |
} | |
} | |
/*===============================================================*/ | |
#endregion | |
/*===============================================================*/ | |
/** | |
* @brief コンストラクタ | |
*/ | |
protected Singleton () {} | |
/*===============================================================*/ | |
} | |
/*===============================================================*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment