Unity事件系统
来源:互联网 发布:工业企业数据库 编辑:程序博客网 时间:2024/05/20 17:59
using System;
using System.Collections.Generic;
using UnityEngine;
public class EventItem
{
private object parameter;
private float time;
private float timer;
private int count;
public void Add(EventBindFunction function)
{
eventBindFunction += function;
}
public void Remove(EventBindFunction function)
{
try
{
eventBindFunction -= function;
}
catch(Exception ex)
{
MyDebug.Instance.LogError(ex + ":卸载不存在事件");
}
}
public void Handle()
{
if (eventBindFunction != null)
eventBindFunction(parameter);
}
}
public class EventManager {
private Dictionary<EventType, EventItem> eventDic = new Dictionary<EventType, EventItem>();
private List<EventItem> eventList = new List<EventItem>();
private EventManager() { }
private static EventManager instance;
public static EventManager Instance
{
get
{
if (instance == null)
instance = new EventManager();
return instance;
}
}
public void RegisterEvent(EventType eventType,EventBindFunction function)
{
if (eventDic.ContainsKey(eventType))
{
eventDic[eventType].Add(function);
}
else
{
EventItem item = new EventItem();
eventDic.Add(eventType, item);
}
}
public void UnRegisterEvent(EventType eventType,EventBindFunction function)
{
if (eventDic.ContainsKey(eventType))
{
EventItem item = eventDic[eventType];
if (eventList.Contains(item))
eventList.Remove(item);
}
else
{
MyDebug.Instance.LogError("卸载不存在事件类型");
}
}
public void FireEvent(EventType eventType,object obj=null)
{
if (eventDic.ContainsKey(eventType))
{
eventDic[eventType].Handle();
}
else
{
MyDebug.Instance.LogError("执行不存在事件类型");
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class EventItem
{
private object parameter;
private float time;
private float timer;
private int count;
public void Add(EventBindFunction function)
{
eventBindFunction += function;
}
public void Remove(EventBindFunction function)
{
try
{
eventBindFunction -= function;
}
catch(Exception ex)
{
MyDebug.Instance.LogError(ex + ":卸载不存在事件");
}
}
public void Handle()
{
if (eventBindFunction != null)
eventBindFunction(parameter);
}
}
public class EventManager {
private Dictionary<EventType, EventItem> eventDic = new Dictionary<EventType, EventItem>();
private List<EventItem> eventList = new List<EventItem>();
private EventManager() { }
private static EventManager instance;
public static EventManager Instance
{
get
{
if (instance == null)
instance = new EventManager();
return instance;
}
}
public void RegisterEvent(EventType eventType,EventBindFunction function)
{
if (eventDic.ContainsKey(eventType))
{
eventDic[eventType].Add(function);
}
else
{
EventItem item = new EventItem();
eventDic.Add(eventType, item);
}
}
public void UnRegisterEvent(EventType eventType,EventBindFunction function)
{
if (eventDic.ContainsKey(eventType))
{
EventItem item = eventDic[eventType];
if (eventList.Contains(item))
eventList.Remove(item);
}
else
{
MyDebug.Instance.LogError("卸载不存在事件类型");
}
}
public void FireEvent(EventType eventType,object obj=null)
{
if (eventDic.ContainsKey(eventType))
{
eventDic[eventType].Handle();
}
else
{
MyDebug.Instance.LogError("执行不存在事件类型");
}
}
}
阅读全文
1 0
- Unity 5事件系统
- Unity事件系统
- Unity UGUI事件系统
- Unity事件系统
- Unity中的事件系统演进
- Unity中的事件系统演进
- Unity中的事件系统历史
- Unity中的事件系统演进
- unity中事件分发系统 EventDispatcher
- unity中事件分发系统 EventDispatcher
- Unity 新UI事件系统(EventSystem) Demo
- unity中事件分发系统 EventDispatcher
- 【Unity】简单的事件分发系统
- Unity UI事件管理系统设计
- Unity动画系统 动态添加动画事件
- unity事件
- Unity UI(十二):UI的事件系统
- Unity游戏UI框架(八):监听事件系统
- 如何写爬虫
- 数据库视频-第一章
- 拥塞控制算法之Verus (2015 Sigcomm)
- 应用缓存获取和清理功能实现失败
- 最小费用最大流 模板
- Unity事件系统
- 十大心理效应
- NVIDIA Jetson TX2编译内核解决无法使用游戏手柄XBOX360的问题
- 51Nod-1181-质数中的质数(质数筛法)
- The Google File System : part8 RELATED WORK and CONCLUSIONS and ACKNOWLEDGMENTS
- C++函数范式
- 3Des加解密,压缩文件
- STL__set让你彻底搞清楚set的特性
- web渗透编码