98 lines
2.1 KiB
C#
98 lines
2.1 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
|
||
#if UNITY_EDITOR
|
||
using UnityEditor.Experimental.GraphView;
|
||
#endif
|
||
|
||
using System.Runtime.CompilerServices;
|
||
|
||
using Xorti = XericLibrary.Runtime.MacroLibrary.MacroMath;
|
||
|
||
using UnityEngine;
|
||
|
||
namespace XericLibrary.Runtime.MacroLibrary
|
||
{
|
||
public static class MacroEvent
|
||
{
|
||
#region 控制器
|
||
|
||
/// <summary>
|
||
/// 保守事件控制器
|
||
/// </summary>
|
||
class ConservativeManager
|
||
{
|
||
#region 字段属性
|
||
|
||
/// <summary>
|
||
/// 事件索引器
|
||
/// </summary>
|
||
private Dictionary<int, int> EventIndex;
|
||
|
||
/// <summary>
|
||
/// 事件状态寄存器
|
||
/// </summary>
|
||
private List<int> eventRegister;
|
||
|
||
#endregion
|
||
|
||
#region 方法
|
||
|
||
public ConservativeManager()
|
||
{
|
||
EventIndex = new Dictionary<int, int>();
|
||
eventRegister = new List<int>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保守地利用控制器调用一个事件分发器
|
||
/// </summary>
|
||
public void InvokeConservative(Delegate targetevent)
|
||
{
|
||
var code = targetevent.GetHashCode();
|
||
int index = 0;
|
||
if(!EventIndex.TryGetValue(code, out index))
|
||
EventIndex.Add(code, EventIndex.Count);
|
||
targetevent.DynamicInvoke();// dynmic代表动态类型,包含装箱拆箱,速度会非常慢
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 事件
|
||
|
||
/// <summary>
|
||
/// 保守地调用一个事件分发器
|
||
/// </summary>
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static bool InvokeConservative(this Action invokedEvent, int sign, ref int register)
|
||
{
|
||
if(Xorti.GetByteState(register, sign))
|
||
return false;
|
||
Xorti.SetByteState(ref register, sign, true);
|
||
invokedEvent?.Invoke();
|
||
Xorti.SetByteState(ref register, sign, false);
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保守地调用一个事件分发器
|
||
/// </summary>
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static bool InvokeConservative<PARAM>(this Action<PARAM> invokedEvent, PARAM input, int sign, ref int register)
|
||
{
|
||
if(Xorti.GetByteState(register, sign))
|
||
return false;
|
||
Xorti.SetByteState(ref register, sign, true);
|
||
invokedEvent?.Invoke(input);
|
||
Xorti.SetByteState(ref register, sign, false);
|
||
return true;
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|