48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BrewMonster.Scripts.Managers
|
|
{
|
|
/// <summary>
|
|
/// Forwards pointer drag events from the GameObject to the nearest parent ScrollRect.
|
|
/// Attach to UI elements (Buttons) that should allow ScrollRect dragging while still receiving clicks.
|
|
/// </summary>
|
|
public sealed class ForwardDragToScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
private ScrollRect _target;
|
|
|
|
public ScrollRect Target
|
|
{
|
|
get
|
|
{
|
|
if (_target == null)
|
|
_target = GetComponentInParent<ScrollRect>();
|
|
return _target;
|
|
}
|
|
set => _target = value;
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
var t = Target;
|
|
if (t != null)
|
|
ExecuteEvents.Execute(t.gameObject, eventData, ExecuteEvents.beginDragHandler);
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
var t = Target;
|
|
if (t != null)
|
|
ExecuteEvents.Execute(t.gameObject, eventData, ExecuteEvents.dragHandler);
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
var t = Target;
|
|
if (t != null)
|
|
ExecuteEvents.Execute(t.gameObject, eventData, ExecuteEvents.endDragHandler);
|
|
}
|
|
}
|
|
}
|