单例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Single<T> where T : class, new()
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
if (_instance == null)
{
_instance = new T();
}
}
return _instance;
}
}
}
消息中心:
using System;
using System.Collections.Generic;
public class MessageManager :Single<MessageManager>
{
Dictionary<int,Action<object>> dic=new Dictionary<int,Action<object>>();
public void Add(int id, Action<object> action)
{
if(dic.ContainsKey(id))
{
dic[id] += action;
}
else
{
dic.Add(id, action);
}
}
public void Remove(int id,Action<object> action)
{
if (dic.ContainsKey(id))
{
dic[id] -= action;
if (dic[id] == null)
{
dic.Remove(id);
}
}
}
public void Send(int id, params object[] arr)
{
if(dic.ContainsKey(id))
{
dic[id](arr);
}
}
}