using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.UI;
|
using UnityEngine.EventSystems;
|
|
public class ScrollRectGroup : MonoBehaviour, IInitializePotentialDragHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
|
{
|
[SerializeField] ScrollRect[] m_ScrollRects;
|
[SerializeField] Direction m_OnlyDirection;
|
[SerializeField] float m_Offset = 100f;
|
|
Dictionary<Direction, ScrollRect> scrollRects = new Dictionary<Direction, ScrollRect>();
|
|
Direction m_Direction = Direction.None;
|
Direction direction
|
{
|
get { return m_Direction; }
|
set
|
{
|
if (value != m_Direction)
|
{
|
m_Direction = value;
|
|
scrollRects[Direction.Horizontal].horizontal = direction == Direction.Horizontal
|
|| direction == Direction.Both;
|
scrollRects[Direction.Vertical].vertical = direction == Direction.Vertical
|
|| direction == Direction.Both;
|
scrollRects[Direction.Both].horizontal = direction == Direction.Horizontal
|
|| direction == Direction.Both;
|
scrollRects[Direction.Both].vertical = direction == Direction.Vertical
|
|| direction == Direction.Both;
|
}
|
}
|
}
|
|
Vector2 m_StartPosition = Vector2.zero;
|
|
|
|
private void Awake()
|
{
|
for (int i = 0; i < m_ScrollRects.Length; i++)
|
{
|
var scrollRect = m_ScrollRects[i];
|
if (scrollRect.horizontal && scrollRect.vertical)
|
{
|
scrollRects.Add(Direction.Both, scrollRect);
|
}
|
else if (scrollRect.horizontal)
|
{
|
scrollRects.Add(Direction.Horizontal, scrollRect);
|
}
|
else
|
{
|
scrollRects.Add(Direction.Vertical, scrollRect);
|
}
|
}
|
|
Initialize();
|
}
|
|
public void Initialize()
|
{
|
direction = Direction.Both;
|
|
scrollRects[Direction.Both].horizontalNormalizedPosition = 0f;
|
scrollRects[Direction.Both].verticalNormalizedPosition = 1f;
|
scrollRects[Direction.Horizontal].horizontalNormalizedPosition = 0f;
|
scrollRects[Direction.Vertical].verticalNormalizedPosition = 1f;
|
}
|
|
public void OnBeginDrag(PointerEventData eventData)
|
{
|
direction = Direction.Both;
|
|
foreach (var scrollRect in m_ScrollRects)
|
{
|
scrollRect.OnBeginDrag(eventData);
|
}
|
|
m_StartPosition = eventData.position;
|
}
|
|
public void OnDrag(PointerEventData eventData)
|
{
|
var position = eventData.position;
|
if (direction == Direction.Both)
|
{
|
if (m_OnlyDirection == Direction.Both)
|
{
|
var horizontal = Mathf.Abs(position.x - m_StartPosition.x) > m_Offset;
|
var vertical = !horizontal && Mathf.Abs(position.y - m_StartPosition.y) > m_Offset;
|
if (horizontal || vertical)
|
{
|
var _direction = horizontal ? Direction.Horizontal : Direction.Vertical;
|
if (_direction != direction)
|
{
|
direction = _direction;
|
foreach (var scrollRect in m_ScrollRects)
|
{
|
scrollRect.OnBeginDrag(eventData);
|
}
|
}
|
}
|
}
|
else
|
{
|
direction = m_OnlyDirection;
|
}
|
}
|
|
if (direction != Direction.Both)
|
{
|
foreach (var scrollRect in m_ScrollRects)
|
{
|
scrollRect.OnDrag(eventData);
|
}
|
}
|
}
|
|
public void OnEndDrag(PointerEventData eventData)
|
{
|
foreach (var scrollRect in m_ScrollRects)
|
{
|
scrollRect.OnEndDrag(eventData);
|
}
|
}
|
|
public void OnInitializePotentialDrag(PointerEventData eventData)
|
{
|
foreach (var scrollRect in m_ScrollRects)
|
{
|
scrollRect.OnInitializePotentialDrag(eventData);
|
}
|
}
|
|
#if UNITY_EDITOR
|
[ContextMenu("CloseRayAccepter")]
|
void BatchCloseRayAccepter()
|
{
|
var componenst = transform.GetComponentsInChildren<Graphic>(true);
|
for (int i = 0; i < componenst.Length; i++)
|
{
|
componenst[i].raycastTarget = false;
|
}
|
}
|
#endif
|
|
enum Direction
|
{
|
None,
|
Horizontal,
|
Vertical,
|
Both,
|
}
|
}
|