using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class NotificationServer : MonoBehaviour
|
|
{
|
|
static Dictionary<string, List<Action>> notifications = new Dictionary<string, List<Action>>();
|
|
static Dictionary<string, List<Action<object>>> notificationsWithArgument = new Dictionary<string, List<Action<object>>>();
|
|
|
|
static public void register(string key, Action action)
|
|
{
|
|
if (!notifications.ContainsKey(key))
|
|
notifications[key] = new List<Action>();
|
|
if (!notifications[key].Contains(action))
|
|
notifications[key].Add(action);
|
|
}
|
|
|
|
static public void unregister(string key, Action action)
|
|
{
|
|
if (!notifications.ContainsKey(key))
|
|
return;
|
|
if (!notifications[key].Contains(action))
|
|
return;
|
|
notifications[key].Remove(action);
|
|
}
|
|
|
|
static public void notify(string key)
|
|
{
|
|
if (!notifications.ContainsKey(key))
|
|
return;
|
|
for (int i=0; i<notifications[key].Count; i++)
|
|
notifications[key][i]();
|
|
}
|
|
|
|
static public void register(string key, Action<object> action)
|
|
{
|
|
if (!notificationsWithArgument.ContainsKey(key))
|
|
notificationsWithArgument[key] = new List<Action<object>>();
|
|
if (!notificationsWithArgument[key].Contains(action))
|
|
notificationsWithArgument[key].Add(action);
|
|
}
|
|
|
|
static public void unregister(string key, Action<object> action)
|
|
{
|
|
if (!notificationsWithArgument.ContainsKey(key))
|
|
return;
|
|
if (!notificationsWithArgument[key].Contains(action))
|
|
return;
|
|
notificationsWithArgument[key].Remove(action);
|
|
}
|
|
|
|
static public void notify(string key, object argument)
|
|
{
|
|
if (!notificationsWithArgument.ContainsKey(key))
|
|
return;
|
|
for (int i=0; i<notificationsWithArgument[key].Count; i++)
|
|
notificationsWithArgument[key][i](argument);
|
|
}
|
|
|
|
public void doNotify(string key)
|
|
{
|
|
notify(key);
|
|
}
|
|
|
|
public void doNotify(string key, string argument)
|
|
{
|
|
notify(key, argument);
|
|
}
|
|
}
|
|
|