Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created March 8, 2014 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/9426445 to your computer and use it in GitHub Desktop.
Save tsubaki/9426445 to your computer and use it in GitHub Desktop.
【試作】変更通知ディクショナリと、シングルトンなデータ管理クラス
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();
}
}
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;
}
}
}
@tsubaki
Copy link
Author

tsubaki commented Mar 8, 2014

値の登録

DataSet.SetBool("FAILD", false);

通知の登録

DataSet.Attatch("FAILD", Clear);
void Clear(bool result){}

通知の解除

DataSet.Detatch("FAILD", Clear);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment