Unity游戏开发 全局消息传递
来源:互联网 发布:保险师软件下载 编辑:程序博客网 时间:2024/06/05 06:40
using System;
using System.Collections;using System.Collections.Generic;
using UnityEngine;
namespace FrameWork
{
/// <summary>
/// delegate Define
/// </summary>
public delegate void GllobalDelegate();
public delegate void GllobalDelegate<T0>(T0 arg0);
public delegate void GllobalDelegate<T0, T1>(T0 arg0, T1 arg1);
public delegate void GllobalDelegate<T0, T1, T2>(T0 arg0, T1 arg1, T2 arg2);
public delegate void GllobalDelegate<T0, T1, T2, T3>(T0 arg0, T1 arg1, T2 arg2, T3 arg3);
public delegate void GllobalDelegate<T0, T1, T2, T3, T4>(T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4);
/// <summary>
/// Gllobal Event Entity
/// </summary>
public class GllobalEventEntity : IDisposable
{
private static GllobalEventEntity _Instance;
private static GllobalEventEntity Instance
{
get
{
if(_Instance==null)
_Instance = new GllobalEventEntity();
return _Instance;
}
set
{
_Instance = value;
}
}
private static Dictionary<System.Enum, Delegate> ListenerDictionary= new Dictionary<System.Enum, Delegate>();
private static void Listen(System.Enum eventType,Delegate Callback)
{
Delegate callback;
if (ListenerDictionary.TryGetValue(eventType, out callback))
{
if ( callback.Method!=Callback.Method)
{
callback = Delegate.Combine(callback, Callback);
ListenerDictionary.Remove(eventType);
ListenerDictionary.Add(eventType, callback);
}
}
else
{
ListenerDictionary.Add(eventType, Callback);
}
}
private static void Remove(System.Enum eventType, Delegate Callback)
{
Delegate callback;
if (ListenerDictionary.TryGetValue(eventType, out callback))
{
callback = Delegate.Remove(callback, Callback);
if (callback == null)
{
if (ListenerDictionary.ContainsKey(eventType))
ListenerDictionary.Remove(eventType);
}
else
{
ListenerDictionary.Remove(eventType);
ListenerDictionary.Add(eventType, callback);
}
}
}
public static void Addlistener(System.Enum eventType, GllobalDelegate Callback)
{
Listen(eventType, Callback);
}
public static void Addlistener<T0>(System.Enum eventType, GllobalDelegate<T0> Callback)
{
Listen(eventType, Callback);
}
public static void Addlistener<T0, T1>(System.Enum eventType, GllobalDelegate<T0, T1> Callback)
{
Listen(eventType, Callback);
}
public static void Addlistener<T0, T1, T2>(System.Enum eventType, GllobalDelegate<T0, T1, T2> Callback)
{
Listen(eventType, Callback);
}
public static void Addlistener<T0, T1, T2, T3>(System.Enum eventType, GllobalDelegate<T0, T1, T2, T3> Callback)
{
Listen(eventType, Callback);
}
public static void Addlistener<T0,T1,T2,T3,T4>(System.Enum eventType, GllobalDelegate<T0,T1,T2,T3,T4> Callback)
{
Listen(eventType, Callback);
}
public static void Removelistener(System.Enum eventType, GllobalDelegate Callback)
{
Remove(eventType, Callback);
}
public static void Removelistener<T0>(System.Enum eventType, GllobalDelegate<T0> Callback)
{
Remove(eventType, Callback);
}
public static void Removelistener<T0, T1>(System.Enum eventType, GllobalDelegate<T0, T1> Callback)
{
Remove(eventType, Callback);
}
public static void Removelistener<T0, T1, T2>(System.Enum eventType, GllobalDelegate<T0, T1, T2> Callback)
{
Remove(eventType, Callback);
}
public static void Removelistener<T0, T1, T2, T3>(System.Enum eventType, GllobalDelegate<T0, T1, T2, T3> Callback)
{
Remove(eventType, Callback);
}
public static void Removelistener<T0, T1, T2, T3, T4>(System.Enum eventType, GllobalDelegate<T0, T1, T2, T3, T4> Callback)
{
Remove(eventType, Callback);
}
public static void Dispatcher(System.Enum eventType)
{
Delegate callback;
if (ListenerDictionary.TryGetValue(eventType, out callback))
{
callback.DynamicInvoke();
}
}
public static void Dispatcher<T0>(System.Enum eventType, T0 arg0)
{
Delegate callback;
if (ListenerDictionary.TryGetValue(eventType, out callback))
{
callback.DynamicInvoke(arg0);
}
}
public static void Dispatcher<T0, T1>(System.Enum eventType, T0 arg0, T1 arg1)
{
Delegate callback;
if (ListenerDictionary.TryGetValue(eventType, out callback))
{
callback.DynamicInvoke(arg0, arg1);
}
}
public static void Dispatcher<T0, T1, T2>(System.Enum eventType, T0 arg0, T1 arg1, T2 arg2)
{
Delegate callback;
if (ListenerDictionary.TryGetValue(eventType, out callback))
{
callback.DynamicInvoke(arg0, arg1, arg2);
}
}
public static void Dispatcher<T0, T1, T2, T3>(System.Enum eventType, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
{
Delegate callback;
if (ListenerDictionary.TryGetValue(eventType, out callback))
{
callback.DynamicInvoke(arg0, arg1, arg2, arg3);
}
}
public static void Dispatcher<T0, T1, T2, T3, T4>(System.Enum eventType, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
Delegate callback;
if (ListenerDictionary.TryGetValue(eventType, out callback))
{
callback.DynamicInvoke(arg0, arg1, arg2, arg3, arg4);
}
}
public void Dispose()
{
ListenerDictionary.Clear();
}
}
}
using FrameWork;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class GllobalEventEntityTest : MonoBehaviour {
/// <summary>
/// Event Type Define
/// </summary>
public enum GllobalEventType
{
Event0,
Event1,
Event2,
}
// Use this for initialization
void Start () {
GllobalEventEntity.Addlistener( GllobalEventType.Event0,Callback0);
GllobalEventEntity.Addlistener(GllobalEventType.Event0, Callback2);
GllobalEventEntity.Addlistener<int>(GllobalEventType.Event1, Callback1);
GllobalEventEntity.Addlistener<int>(GllobalEventType.Event1, Callback4);
}
private void Callback4(int arg0)
{
Debug.Log("Callback4 " + arg0.ToString());
}
private void Callback2()
{
Debug.Log("Callback2");
}
private void Callback1(int arg0)
{
Debug.Log("Callback1 "+ arg0.ToString());
}
private void Callback0()
{
Debug.Log("Callback0");
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
Start();
}
if (Input.GetKeyDown(KeyCode.F1))
{
GllobalEventEntity.Dispatcher(GllobalEventType.Event0);
}
if (Input.GetKeyDown(KeyCode.F2))
{
GllobalEventEntity.Dispatcher<int>(GllobalEventType.Event1,11);
}
if (Input.GetKeyDown(KeyCode.F3))
{
GllobalEventEntity.Removelistener(GllobalEventType.Event0, Callback2);
GllobalEventEntity.Removelistener<int>(GllobalEventType.Event1, Callback4);
}
}
}
- Unity游戏开发 全局消息传递
- Unity中游戏体的消息传递
- Unity游戏UI框架(七):消息传递中心
- 【Cocos2dx游戏开发】CCNotificationCenter传递消息和数据
- VC开发-------消息传递
- unity 游戏开发 链接
- Unity游戏开发倒计时
- Unity与游戏开发
- UNITY游戏开发源码
- Unity的三种消息传递
- Unity3D:unity与Android相互传递消息 & unity与ios相互传递消息
- chrome插件开发----消息传递
- 【Unity游戏开发】浅谈Unity游戏开发中的单元测试
- Unity 全局游戏脚本的一种实现方法
- 游戏开发-Unity中的音频
- Unity游戏开发实战视频教程
- Unity开发MMOARPG游戏解决方案
- 《Unity 游戏开发技巧集锦》
- IDEA :插入代码模板(Ctrl+J )
- react-native-router-flux popTo('想pop到的scene') 回退多级不能实现问题
- c++之友元
- Codeforces Gym
- 列表有关
- Unity游戏开发 全局消息传递
- (python3)1077. 互评成绩计算 (20分)——2017-12考题
- 浅析 JavaScript 执行原理
- Gobblin作为嵌入应用(Library)
- [论文笔记]TextBoxes A Fast Text Detector with a Single Deep Neural Network
- 《Java编程思想》第0记
- MyBatis动态语句及利用Map类来传递参数
- MIT-BIH心率失常数据提取及部分MATLAB程序解释
- 插入排序.c