using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 观察者模式
/// by:zdz
/// date:20220328
///
public class Dispatcher : Singleton
{
public delegate void ActionHandler(params object[] param);
private Dictionary handlerDictionary = new Dictionary();
///
/// 增加监听
///
///
///
public void AddListener(int id, ActionHandler handler)
{
if (handlerDictionary.ContainsKey(id))
{
handlerDictionary[id] += handler;
}
else
{
handlerDictionary.Add(id, handler);
}
}
///
/// 移除监听
///
///
///
public void RemoveListener(int id, ActionHandler handler)
{
if (handlerDictionary.ContainsKey(id))
{
if (handlerDictionary[id] != null)
{
handlerDictionary[id] -= handler;
}
else
{
handlerDictionary.Remove(id);
}
}
}
///
/// 派发
///
///
///
public void Dispatch(int id, params object[] param)
{
if (handlerDictionary.ContainsKey(id))
{
if (handlerDictionary[id] != null)
{
handlerDictionary[id](param);
}
}
}
}