using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MessageCenter
{
//
public delegate void DelMessageDelivery(KeyValueUpdate kv);
//
public static Dictionary _dicMessages = new Dictionary();
///
///
///
///
///
public static void AddMsgListener(string messageType, DelMessageDelivery handler)
{
// ,
if (!_dicMessages.ContainsKey(messageType))
{
_dicMessages.Add(messageType, null);
}
_dicMessages[messageType] += handler;
}
///
///
///
///
///
public static void RemoveMsgListener(string messageType, DelMessageDelivery handler)
{
// ,
if (_dicMessages.ContainsKey(messageType))
{
_dicMessages[messageType] -= handler;
}
}
///
///
///
///
public static void RemoveAllMsgListener()
{
// ,
if (_dicMessages != null)
{
_dicMessages.Clear();
}
}
///
///
///
///
///
public static void SendMessage(string messageType, KeyValueUpdate kv)
{
DelMessageDelivery del;//
if (_dicMessages.TryGetValue(messageType,out del))
{
if (del!=null)
{
//
del(kv);
}
}
}
}
///
/// ,
///
public class KeyValueUpdate
{
private string _Key;
private object _Values;
public string Key
{
get { return _Key; }
}
public object Values
{
get { return _Values; }
}
public KeyValueUpdate(string key, object valueObj)
{
_Key = key;
_Values = valueObj;
}
}
よびだし using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Main : MonoBehaviour
{
public KeyValueUpdate MyData = new KeyValueUpdate("Chh", 123);
void Start()
{
MessageCenter.AddMsgListener("Chh", MyButton);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
MessageCenter.SendMessage("Chh",MyData);
}
}
public void MyButton(KeyValueUpdate kv)
{
Debug.Log(kv.Key+""+kv.Values.ToString());
}
}