yyl
2025-05-19 b118ece3db250a5a257b60713da92234d8d5a57a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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,
    }
}