单例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singleton<T>where T : class,new()
{
private static T Ins;
public static T Instance
{
get
{
if(Ins == null)
{
Ins = new T();
}
return Ins;
}
}
}
消息中心
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MessageManager<T> : Singleton<MessageManager<T>>
{
Dictionary<int,Action<T>> dic=new Dictionary<int,Action<T>>();
public void OnAddListen(int id, Action<T> action)
{
if(dic.ContainsKey(id))
{
dic[id] += action;
}
else
{
dic.Add(id, action);
}
}
public void OnRemoveListen(int id)
{
if(dic.ContainsKey(id))
{
dic.Remove(id);
}
}
public void OnBroCast(int id,T a)
{
if(dic.ContainsKey(id))
{
dic[id](a);
}
}
}