在垂直滚动窗体中,水平滚动的窗体组件
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class MyScrollRect : ScrollRect
{
private MyScrollRect m_Parent;
public enum Direction
{
Horizontal,
Vertical
}
private Direction m_Direction = Direction.Horizontal;
private Direction m_BeginDragDirection = Direction.Horizontal;
protected override void Awake()
{
base.Awake();
Transform parent = transform.parent;
if (parent)
{
m_Parent = parent.GetComponentInParent<MyScrollRect>();
}
m_Direction = this.horizontal ? Direction.Horizontal : Direction.Vertical;
}
public override void OnBeginDrag(PointerEventData eventData)
{
if (m_Parent)
{
m_BeginDragDirection = Mathf.Abs(eventData.delta.x) > Mathf.Abs(eventData.delta.y) ? Direction.Horizontal : Direction.Vertical;
if (m_BeginDragDirection != m_Direction)
{
ExecuteEvents.Execute(m_Parent.gameObject, eventData, ExecuteEvents.beginDragHandler);
return;
}
}
base.OnBeginDrag(eventData);
}
public override void OnDrag(PointerEventData eventData)
{
if (m_Parent)
{
if (m_BeginDragDirection != m_Direction)
{
ExecuteEvents.Execute(m_Parent.gameObject, eventData, ExecuteEvents.dragHandler);
return;
}
}
base.OnDrag(eventData);
}
public override void OnEndDrag(PointerEventData eventData)
{
if (m_Parent)
{
if (m_BeginDragDirection != m_Direction)
{
ExecuteEvents.Execute(m_Parent.gameObject, eventData, ExecuteEvents.endDragHandler);
return;
}
}
base.OnEndDrag(eventData);
}
public override void OnScroll(PointerEventData data)
{
if (m_Parent)
{
if (m_BeginDragDirection != m_Direction)
{
ExecuteEvents.Execute(m_Parent.gameObject, data, ExecuteEvents.scrollHandler);
return;
}
}
base.OnScroll(data);
}
}
|