157 lines
5.2 KiB
C#
157 lines
5.2 KiB
C#
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace AibisDream.Framework
|
|
{
|
|
/// <summary>
|
|
/// EventTrigger扩展
|
|
/// </summary>
|
|
[RequireComponent(typeof(Collider2D))]
|
|
public class EventTriggerEx : EventTrigger
|
|
{
|
|
private IInteraction _interaction;
|
|
|
|
private void Awake()
|
|
{
|
|
_interaction = GetComponent<IInteraction>();
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
if (_interaction != null) EventSystemEx.Instance.OnPointerEnter(_interaction);
|
|
base.OnPointerEnter(data);
|
|
}
|
|
|
|
public override void OnPointerExit(PointerEventData data)
|
|
{
|
|
if (_interaction != null) EventSystemEx.Instance.OnPointerExit(_interaction);
|
|
base.OnPointerExit(data);
|
|
}
|
|
|
|
public override void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
// 对话等场景 isLocked 时仍要让 EventTrigger 上注册的回调执行(如塔罗 PointerDown),
|
|
// 否则会出现能 Hover、但 Down/Up/Click 全不进组件的问题。
|
|
if (_interaction != null && _interaction.IsAvailable && !EventSystemEx.Instance.isLocked)
|
|
EventSystemEx.Instance.OnPointerDown(_interaction);
|
|
base.OnPointerDown(eventData);
|
|
}
|
|
|
|
public override void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
if (_interaction != null && _interaction.IsAvailable && !EventSystemEx.Instance.isLocked)
|
|
EventSystemEx.Instance.OnPointerUp(_interaction);
|
|
base.OnPointerUp(eventData);
|
|
}
|
|
|
|
public override void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (_interaction != null && _interaction.IsAvailable && !EventSystemEx.Instance.isLocked)
|
|
{
|
|
EventSystemEx.Instance.OnDrag(_interaction);
|
|
base.OnDrag(eventData);
|
|
}
|
|
}
|
|
|
|
public override void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
if (_interaction != null && _interaction.IsAvailable && !EventSystemEx.Instance.isLocked)
|
|
{
|
|
EventSystemEx.Instance.OnEndDrag(_interaction);
|
|
base.OnEndDrag(eventData);
|
|
}
|
|
}
|
|
|
|
public override void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
if (_interaction != null && _interaction.IsAvailable && !EventSystemEx.Instance.isLocked)
|
|
{
|
|
EventSystemEx.Instance.OnBeginDrag(_interaction);
|
|
base.OnBeginDrag(eventData);
|
|
}
|
|
}
|
|
|
|
public override void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
if (_interaction != null && _interaction.IsAvailable && !EventSystemEx.Instance.isLocked)
|
|
{
|
|
// EventSystemEx.Instance.OnPointerClick(_interaction);
|
|
}
|
|
base.OnPointerClick(eventData);
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
EventSystemEx.Instance.UnRegister(_interaction);
|
|
}
|
|
}
|
|
|
|
public interface IInteraction
|
|
{
|
|
// 判断是否激活,激活就正常显示鼠标,否则就显示禁用标志
|
|
public bool IsActive { get; }
|
|
|
|
// 判断是否可用,可用就正常显示鼠标,否则就保持原有标志不变
|
|
public bool IsAvailable { get; }
|
|
|
|
// 返回GameObject
|
|
public GameObject GetGameObject();
|
|
}
|
|
} |