Created
March 8, 2014 06:45
-
-
Save tsubaki/9426445 to your computer and use it in GitHub Desktop.
【試作】変更通知ディクショナリと、シングルトンなデータ管理クラス
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; | |
public class DataSet : SingletonMonoBehaviour<DataSet> { | |
public ObserverDictionary<bool> notiBool = new ObserverDictionary<bool>(); | |
public ObserverDictionary<int> notiInt = new ObserverDictionary<int>(); | |
public static void Attatch(string key, ObserverDictionary<bool>.NotificationAction action) | |
{ | |
Instance.notiBool.AddObserver(key, action); | |
} | |
public static void Attatch(string key, ObserverDictionary<int>.NotificationAction action) | |
{ | |
Instance.notiInt.AddObserver(key, action); | |
} | |
public static void Detatch(string key, ObserverDictionary<bool>.NotificationAction action) | |
{ | |
if( Instance != null) | |
Instance.notiBool.RemoveObserver(key, action); | |
} | |
public static void Detatch(string key, ObserverDictionary<int>.NotificationAction action) | |
{ | |
if( Instance != null) | |
Instance.notiInt.RemoveObserver(key, action); | |
} | |
public static int GetInt(string key) | |
{ | |
return Instance.notiInt[key]; | |
} | |
public static bool GetBool(string key) | |
{ | |
return Instance.notiBool[key]; | |
} | |
public static void SetInt(string key, int value) | |
{ | |
Instance.notiInt[key] = value; | |
} | |
public static void SetBool(string key, bool value) | |
{ | |
Instance.notiBool[key] = value; | |
} | |
void OnDestroy() | |
{ | |
notiBool.Dispose(); | |
notiInt.Dispose(); | |
} | |
} |
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; | |
public class ObserverDictionary<T> : IDisposable where T : System.IComparable | |
{ | |
public delegate void NotificationAction (T t); | |
private Dictionary<string, NotificationClass> dic = new Dictionary<string, NotificationClass> (); | |
public T this [string key] { | |
get { | |
return CheckKey (key).data; | |
} | |
set { | |
NotificationClass param = CheckKey (key); | |
if (param.data.CompareTo (value) != 0) { | |
param.data = value; | |
param.Play (); | |
} | |
} | |
} | |
public int Count { | |
get { | |
return dic.Count; | |
} | |
} | |
public void AddObserver (string key, NotificationAction action) | |
{ | |
NotificationClass param = CheckKey (key); | |
param.Add (action); | |
} | |
public void RemoveObserver (string key, NotificationAction action) | |
{ | |
NotificationClass param = CheckKey (key); | |
param.Remove (action); | |
} | |
public void Dispose () | |
{ | |
foreach (var item in dic.Values) { | |
item.Clear (); | |
} | |
dic.Clear (); | |
} | |
protected NotificationClass CheckKey (string key) | |
{ | |
if (dic.ContainsKey (key)) { | |
return dic [key]; | |
} else { | |
NotificationClass param = new NotificationClass (); | |
dic [key] = param; | |
return param; | |
} | |
} | |
protected class NotificationClass | |
{ | |
public T data; | |
public event NotificationAction action; | |
public void Play () | |
{ | |
if (action != null) | |
action (data); | |
} | |
public void Clear () | |
{ | |
action = null; | |
} | |
public void Add (NotificationAction act) | |
{ | |
action += act; | |
} | |
public void Remove (NotificationAction act) | |
{ | |
action -= act; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
値の登録
通知の登録
通知の解除