Files
Main/Assets/Launcher/ExternalLibs/EventProxy/EventManagerProxy.cs
2025-01-25 04:38:09 +08:00

100 lines
3.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
using EventSystemHandler = System.Action<System.Object, System.Object>;
namespace Thousandto.Launcher.ExternalLibs
{
//事件管理器EventMamanger的代理用于Launcher中的脚本操作发送事件给逻辑系统
public class EventManagerProxy
{
//事件类型
private static Type _type;
//代理类
private static Type _delegateType;
//push消息的方法
private static MethodInfo _miPushEvent;
//注册消息的方法
private static MethodInfo _miRegEvent;
//取消注册的方法
private static MethodInfo _miUnRegEvent;
//事件管理器的实例对象
private static object _eventManagerObj;
//初始化类型
private static void InitType()
{
if (_eventManagerObj == null)
{
var assmbles = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assmbles.Length; i++)
{
var t = assmbles[i].GetType("Thousandto.Core.Base.EventManager");
if (t != null)
{
_type = t;
_delegateType = assmbles[i].GetType("Thousandto.Core.Base.EventSystemHandler");
var pi = t.GetProperty("SharedInstance", BindingFlags.Static | BindingFlags.Public);
_eventManagerObj = pi.GetValue(null, null);
_miPushEvent = t.GetMethod("PushFixEvent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod);
_miRegEvent = t.GetMethod("RegFixEventHandle", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod);
_miUnRegEvent = t.GetMethod("UnRegFixEventHandle", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod);
return;
}
}
}
}
//发送事件消息
public static void PushFixEvent(int eventType, object param = null, object sender = null, object target = null, bool isSync = true, float delayTime = 0f)
{
InitType();
if (_eventManagerObj != null && _miPushEvent != null)
{
_miPushEvent.Invoke(_eventManagerObj, new object[] { eventType, param, sender, target, isSync, delayTime });
}
else
{
Debug.Log("Laucnher访问给事件系统失败因为逻辑系统还没有被初始化PushFixEvent");
}
}
//注册事件
public static void RegFixEventHandle(int eventType, EventSystemHandler eventHandler)
{
InitType();
if (_eventManagerObj != null && _miRegEvent != null && eventHandler != null)
{
_miRegEvent.Invoke(_eventManagerObj, new object[] { eventType, Delegate.CreateDelegate(_delegateType, eventHandler.Target,eventHandler.Method) });
}
else
{
Debug.Log("Laucnher访问给事件系统失败因为逻辑系统还没有被初始化RegFixEventHandle");
}
}
//取消某个注册消息
public static void UnRegFixEventHandle(int eventType, EventSystemHandler eventHandler)
{
InitType();
if (_eventManagerObj != null && _miUnRegEvent != null && eventHandler != null)
{
_miUnRegEvent.Invoke(_eventManagerObj, new object[] { eventType, Delegate.CreateDelegate(_delegateType, eventHandler.Target, eventHandler.Method) });
}
else
{
Debug.Log("Laucnher访问给事件系统失败因为逻辑系统还没有被初始化UnRegFixEventHandle");
}
}
}
}