事件系统开发中
This commit is contained in:
8
Assets/GameFramework/Runtime/EventSystem.meta
Normal file
8
Assets/GameFramework/Runtime/EventSystem.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 26cf5e3096559174b90385ee2f3d88b0
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
88
Assets/GameFramework/Runtime/EventSystem/EventBus.cs
Normal file
88
Assets/GameFramework/Runtime/EventSystem/EventBus.cs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public static class EventBus
|
||||||
|
{
|
||||||
|
|
||||||
|
private static readonly Dictionary<Type, object> _eventHandlers = new Dictionary<Type, object>();
|
||||||
|
private static readonly object _lock = new object();
|
||||||
|
|
||||||
|
public static void Register<TEvent>(IEventHandler<TEvent> handler) where TEvent : IEvent
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
Type eventType = typeof(TEvent);
|
||||||
|
if (!_eventHandlers.ContainsKey(eventType))
|
||||||
|
{
|
||||||
|
_eventHandlers[eventType] = new List<IEventHandler<TEvent>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var handlers = _eventHandlers[eventType] as List<IEventHandler<TEvent>>;
|
||||||
|
if (handler != null && !handlers.Contains(handler))
|
||||||
|
{
|
||||||
|
handlers.Add(handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Unregister<TEvent>(IEventHandler<TEvent> handler) where TEvent : IEvent
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
Type eventType = typeof(TEvent);
|
||||||
|
if (_eventHandlers.ContainsKey(eventType))
|
||||||
|
{
|
||||||
|
var handlers = _eventHandlers[eventType] as List<IEventHandler<TEvent>>;
|
||||||
|
handlers?.Remove(handler);
|
||||||
|
|
||||||
|
if (handlers != null && handlers.Count == 0)
|
||||||
|
{
|
||||||
|
_eventHandlers.Remove(eventType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Publish<TEvent>(TEvent eventData) where TEvent : IEvent
|
||||||
|
{
|
||||||
|
List<IEventHandler<TEvent>> handlersToInvoke = null;
|
||||||
|
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
Type eventType = typeof(TEvent);
|
||||||
|
if (_eventHandlers.ContainsKey(eventType))
|
||||||
|
{
|
||||||
|
var handlers = _eventHandlers[eventType] as List<IEventHandler<TEvent>>;
|
||||||
|
if (handlers != null && handlers.Count > 0)
|
||||||
|
{
|
||||||
|
handlersToInvoke = new List<IEventHandler<TEvent>>(handlers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在锁外执行事件处理,避免死锁
|
||||||
|
if (handlersToInvoke != null)
|
||||||
|
{
|
||||||
|
foreach (var handler in handlersToInvoke)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
handler?.HandleEvent(eventData);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogError($"Event handling error in {handler.GetType().Name}: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Clear()
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
_eventHandlers.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 182f32b2a26cc364fa0e982e86b2a730
|
||||||
27
Assets/GameFramework/Runtime/EventSystem/IEvent.cs
Normal file
27
Assets/GameFramework/Runtime/EventSystem/IEvent.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
public interface IEvent { }
|
||||||
|
|
||||||
|
public interface IEventHandler<TEvent> where TEvent : IEvent
|
||||||
|
{
|
||||||
|
void HandleEvent(TEvent eventData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct ApplicationFocusEvent : IEvent
|
||||||
|
{
|
||||||
|
public bool HasFocus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct SceneLoadEvent : IEvent
|
||||||
|
{
|
||||||
|
public string SceneName;
|
||||||
|
public float Progress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class EventHandler<TEvent> : IEventHandler<TEvent> where TEvent : IEvent
|
||||||
|
{
|
||||||
|
public void HandleEvent(TEvent eventData)
|
||||||
|
{
|
||||||
|
OnEvent(eventData);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void OnEvent(TEvent eventData);
|
||||||
|
}
|
||||||
2
Assets/GameFramework/Runtime/EventSystem/IEvent.cs.meta
Normal file
2
Assets/GameFramework/Runtime/EventSystem/IEvent.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e7820fef43b561e4ea2ac8370cb7e831
|
||||||
Reference in New Issue
Block a user