内容へ移動
ユーザ用ツール
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
現在位置:
Home
»
wiki
»
Unity
»
Tips
»
059 シングルトンクラスについて
トレース:
wiki:unity:tips:059
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== 059 シングルトンクラスについて ====== ===== 概要 ===== Unityでのシングルトンクラスです。 ==== ソースコード ==== <code csharp> using UnityEngine; using System.Collections; //------------------------------------------------------------------------------ // シングルトン //------------------------------------------------------------------------------ public abstract class Singleton<T> : MonoBehaviour where T : Singleton<T> { //------------------------------------------------------------------ // member //------------------------------------------------------------------ private static T m_Instance = null; //------------------------------------------------------------------ // インスタンス取得 //------------------------------------------------------------------ public static T Instance { get { if( m_Instance == null ) { m_Instance = (T)FindObjectOfType(typeof(T) ); if( m_Instance == null ) { Debug.LogError( typeof(T) + "is nothing" ); } } return m_Instance; } } //------------------------------------------------------------------ // 初期化 //------------------------------------------------------------------ protected abstract void Initialize(); //------------------------------------------------------------------ // Awake //------------------------------------------------------------------ private void Awake() { if( m_Instance != null && m_Instance != (T)this ) { Destroy(gameObject); return; } Initialize(); DontDestroyOnLoad( gameObject ); m_Instance = (T)this; } } </code> ==== 使い方 ==== <code csharp> public class Fader : Singleton<Fader> { protected override void Initialize() { // TODO } } </code>
Permalink
wiki/unity/tips/059.txt
· 最終更新: 2014/11/13 08:25 (外部編集)
ページ用ツール
ログイン
文書の表示
文書の先頭へ
印刷
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
oeffentlich