132 lines
4.4 KiB
C#
132 lines
4.4 KiB
C#
|
|
using System;
|
|||
|
|
using AlicizaX.Runtime;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace AlicizaX.Event.Runtime
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 事件组件。
|
|||
|
|
/// </summary>
|
|||
|
|
[DisallowMultipleComponent]
|
|||
|
|
[AddComponentMenu("Game Framework/Event")]
|
|||
|
|
[UnityEngine.Scripting.Preserve]
|
|||
|
|
public sealed class EventComponent : GameFrameworkComponent
|
|||
|
|
{
|
|||
|
|
private IEventManager m_EventManager = null;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取事件处理函数的数量。
|
|||
|
|
/// </summary>
|
|||
|
|
public int EventHandlerCount
|
|||
|
|
{
|
|||
|
|
get { return m_EventManager.EventHandlerCount; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取事件数量。
|
|||
|
|
/// </summary>
|
|||
|
|
public int EventCount
|
|||
|
|
{
|
|||
|
|
get { return m_EventManager.EventCount; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 游戏框架组件初始化。
|
|||
|
|
/// </summary>
|
|||
|
|
protected override void Awake()
|
|||
|
|
{
|
|||
|
|
ImplementationComponentType = Utility.Assembly.GetType(componentType);
|
|||
|
|
InterfaceComponentType = typeof(IEventManager);
|
|||
|
|
base.Awake();
|
|||
|
|
m_EventManager = SysModuleCenter.GetModule<IEventManager>();
|
|||
|
|
if (m_EventManager == null)
|
|||
|
|
{
|
|||
|
|
Log.Fatal("Event manager is invalid.");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取事件处理函数的数量。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">事件类型编号。</param>
|
|||
|
|
/// <returns>事件处理函数的数量。</returns>
|
|||
|
|
public int Count(string id)
|
|||
|
|
{
|
|||
|
|
return m_EventManager.Count(id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 检查是否存在事件处理函数。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">事件类型编号。</param>
|
|||
|
|
/// <param name="handler">要检查的事件处理函数。</param>
|
|||
|
|
/// <returns>是否存在事件处理函数。</returns>
|
|||
|
|
public bool Check(string id, EventHandler<GameEventArgs> handler)
|
|||
|
|
{
|
|||
|
|
return m_EventManager.Check(id, handler);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 检查订阅事件处理回调函数。当不存在时自动订阅
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">事件类型编号。</param>
|
|||
|
|
/// <param name="handler">要订阅的事件处理回调函数。</param>
|
|||
|
|
public void CheckSubscribe(string id, EventHandler<GameEventArgs> handler)
|
|||
|
|
{
|
|||
|
|
if (!Check(id, handler))
|
|||
|
|
{
|
|||
|
|
m_EventManager.Subscribe(id, handler);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 订阅事件处理回调函数。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">事件类型编号。</param>
|
|||
|
|
/// <param name="handler">要订阅的事件处理回调函数。</param>
|
|||
|
|
public void Subscribe(string id, EventHandler<GameEventArgs> handler)
|
|||
|
|
{
|
|||
|
|
m_EventManager.Subscribe(id, handler);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 取消订阅事件处理回调函数。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">事件类型编号。</param>
|
|||
|
|
/// <param name="handler">要取消订阅的事件处理回调函数。</param>
|
|||
|
|
public void Unsubscribe(string id, EventHandler<GameEventArgs> handler)
|
|||
|
|
{
|
|||
|
|
m_EventManager.Unsubscribe(id, handler);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设置默认事件处理函数。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="handler">要设置的默认事件处理函数。</param>
|
|||
|
|
public void SetDefaultHandler(EventHandler<GameEventArgs> handler)
|
|||
|
|
{
|
|||
|
|
m_EventManager.SetDefaultHandler(handler);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 抛出事件,这个操作是线程安全的,即使不在主线程中抛出,也可保证在主线程中回调事件处理函数,但事件会在抛出后的下一帧分发。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="sender">事件发送者。</param>
|
|||
|
|
/// <param name="e">事件内容。</param>
|
|||
|
|
public void Fire(object sender, GameEventArgs e)
|
|||
|
|
{
|
|||
|
|
m_EventManager.Fire(sender, e);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 抛出事件立即模式,这个操作不是线程安全的,事件会立刻分发。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="sender">事件发送者。</param>
|
|||
|
|
/// <param name="e">事件内容。</param>
|
|||
|
|
public void FireNow(object sender, GameEventArgs e)
|
|||
|
|
{
|
|||
|
|
m_EventManager.FireNow(sender, e);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|