EventSystem扩展
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using AibisDream.Kit;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream.Framework
|
||||
{
|
||||
public class EventSystemEx : SingletonBase<EventSystemEx>
|
||||
{
|
||||
public GameObject pointerOverObj;
|
||||
public GameObject draggingObj;
|
||||
public GameObject holdingObj;
|
||||
|
||||
#region 事件触发中转
|
||||
|
||||
public void OnPointerEnter(GameObject gameObject)
|
||||
{
|
||||
pointerOverObj = gameObject;
|
||||
EnumEventSystem.Global.Send(TriggerEnum.PointerEnter, gameObject);
|
||||
}
|
||||
|
||||
public void OnPointerExit(GameObject gameObject)
|
||||
{
|
||||
EnumEventSystem.Global.Send(TriggerEnum.PointerExit, gameObject);
|
||||
if (pointerOverObj == gameObject)
|
||||
{
|
||||
pointerOverObj = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerDown(GameObject gameObject)
|
||||
{
|
||||
EnumEventSystem.Global.Send(TriggerEnum.PointerDown, gameObject);
|
||||
holdingObj = gameObject;
|
||||
}
|
||||
|
||||
public void OnPointerUp(GameObject gameObject)
|
||||
{
|
||||
EnumEventSystem.Global.Send(TriggerEnum.PointerUp, gameObject);
|
||||
if (holdingObj == gameObject)
|
||||
{
|
||||
holdingObj = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 单例必须的代码
|
||||
|
||||
private EventSystemEx()
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnSingletonInit()
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public enum TriggerEnum
|
||||
{
|
||||
PointerEnter,
|
||||
PointerExit,
|
||||
PointerDown,
|
||||
PointerUp
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f55689aa6dac4461bba273d8987762dd
|
||||
timeCreated: 1737014094
|
||||
@@ -0,0 +1,86 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace AibisDream.Framework
|
||||
{
|
||||
/// <summary>
|
||||
/// EventTrigger扩展
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class EventTriggerEx : EventTrigger
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册事件
|
||||
/// </summary>
|
||||
/// <param name="triggerType">事件类型</param>
|
||||
/// <param name="action">函数</param>
|
||||
public void Register(EventTriggerType triggerType, UnityAction<BaseEventData> action)
|
||||
{
|
||||
if (TryGetTriggerEvent(triggerType, out var triggerEvent))
|
||||
{
|
||||
triggerEvent.AddListener(action);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"{triggerType.ToString()}未添加");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 卸载事件
|
||||
/// </summary>
|
||||
/// <param name="triggerType">事件类型</param>
|
||||
/// <param name="action">函数</param>
|
||||
public void UnRegister(EventTriggerType triggerType, UnityAction<BaseEventData> action)
|
||||
{
|
||||
if (TryGetTriggerEvent(triggerType, out var triggerEvent))
|
||||
{
|
||||
triggerEvent.RemoveListener(action);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"{triggerType.ToString()}未添加");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取触发事件
|
||||
/// </summary>
|
||||
/// <param name="triggerType">事件类型</param>
|
||||
/// <param name="triggerEvent">事件</param>
|
||||
/// <returns>是否存在</returns>
|
||||
private bool TryGetTriggerEvent(EventTriggerType triggerType, out TriggerEvent triggerEvent)
|
||||
{
|
||||
var entry = triggers.FirstOrDefault(item => item.eventID == triggerType);
|
||||
if (entry == null)
|
||||
{
|
||||
triggerEvent = null;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
triggerEvent = entry.callback;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnPointerEnter(PointerEventData data)
|
||||
{
|
||||
EventSystemEx.Instance.OnPointerEnter(gameObject);
|
||||
base.OnPointerEnter(data);
|
||||
}
|
||||
|
||||
public override void OnPointerExit(PointerEventData data)
|
||||
{
|
||||
EventSystemEx.Instance.OnPointerExit(gameObject);
|
||||
base.OnPointerExit(data);
|
||||
}
|
||||
|
||||
public override void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c4fbab01ad40c2449c849cd2a8c620f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user