lcy
2026-07-16 34179ffa10e510ba71522cb2bd1fd978b7fcbbe1
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
 
public interface ICarouselItem
{
    RectTransform RectTransform { get; }
    void SetSelected(bool selected);
}
 
public abstract class CarouselView<TItem, TData> : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
    where TItem : MonoBehaviour, ICarouselItem
{
    public event Action OnSelectedIndexChanged;
 
    readonly List<TItem> items = new List<TItem>();
    List<TData> dataList;
 
    float currentCenterOffset;
    float targetCenterOffset;
    bool isDragging;
    int currentSelectedIndex;
 
    protected abstract RectTransform Content { get; }
    protected abstract GameObject ItemPrefab { get; }
    protected abstract float ItemSpacing { get; }
    protected abstract float ScaleMax { get; }
    protected abstract float ScaleMin { get; }
    protected virtual int PoolSize => 7;
    protected int CurrentSelectedIndex => currentSelectedIndex;
    protected IReadOnlyList<TData> DataList => dataList;
 
    public void InitPool()
    {
        if (items.Count > 0)
            return;
 
        for (int i = 0; i < PoolSize; i++)
        {
            GameObject go = Instantiate(ItemPrefab, Content);
            TItem item = go.GetComponent<TItem>();
            if (item == null)
                continue;
            items.Add(item);
        }
    }
 
    public void InitWithIndex(int index)
    {
        if (dataList.IsNullOrEmpty())
            return;
 
        index = Mathf.Clamp(index, 0, dataList.Count - 1);
        currentSelectedIndex = index;
        currentCenterOffset = index;
        targetCenterOffset = index;
        UpdateItems();
    }
 
    public void SelectIndex(int index)
    {
        if (dataList.IsNullOrEmpty())
            return;
 
        index = GetRealIndex(index);
 
        int oldIndex = currentSelectedIndex;
        currentSelectedIndex = index;
        targetCenterOffset = index;
        currentCenterOffset = index;
        UpdateItems();
 
        if (oldIndex != currentSelectedIndex)
            OnSelectedIndexChanged?.Invoke();
    }
 
    public void SelectPrevious()
    {
        if (dataList.IsNullOrEmpty())
            return;
 
        int newIndex = (currentSelectedIndex - 1 + dataList.Count) % dataList.Count;
        SelectIndex(newIndex);
    }
 
    public void SelectNext()
    {
        if (dataList.IsNullOrEmpty())
            return;
 
        int newIndex = (currentSelectedIndex + 1) % dataList.Count;
        SelectIndex(newIndex);
    }
 
    public void RefreshAllItems()
    {
        if (!dataList.IsNullOrEmpty())
            UpdateItems();
    }
 
    protected void SetDataList(List<TData> newDataList, int selectedIndex)
    {
        dataList = newDataList;
 
        if (dataList.IsNullOrEmpty())
        {
            HideAllItems();
            return;
        }
 
        InitWithIndex(selectedIndex);
    }
 
    protected bool TryGetCurrentData(out TData data)
    {
        data = default(TData);
        if (dataList.IsNullOrEmpty() || currentSelectedIndex < 0)
            return false;
 
        int realIndex = GetRealIndex(currentSelectedIndex);
        data = dataList[realIndex];
        return true;
    }
 
    protected void HideAllItems()
    {
        for (int i = 0; i < items.Count; i++)
        {
            if (items[i] != null)
                items[i].gameObject.SetActive(false);
        }
    }
 
    protected virtual void OnDestroy()
    {
        items.Clear();
    }
 
    protected virtual void Update()
    {
        if (!isDragging && Mathf.Abs(currentCenterOffset - targetCenterOffset) > 0.001f)
        {
            currentCenterOffset = Mathf.Lerp(currentCenterOffset, targetCenterOffset, Time.deltaTime * 10f);
            if (Mathf.Abs(currentCenterOffset - targetCenterOffset) <= 0.001f)
                currentCenterOffset = targetCenterOffset;
            UpdateItems();
        }
    }
 
    void UpdateItems()
    {
        if (dataList.IsNullOrEmpty())
            return;
 
        int poolSize = items.Count;
        int startOffset = Mathf.FloorToInt(currentCenterOffset) - (poolSize / 2);
        int selectedLogicIndex = Mathf.RoundToInt(targetCenterOffset);
 
        for (int i = 0; i < poolSize; i++)
        {
            int logicIndex = startOffset + i;
            int realIndex = GetRealIndex(logicIndex);
 
            float distanceToCenter = logicIndex - currentCenterOffset;
            float posX = distanceToCenter * ItemSpacing;
 
            TItem itemUI = items[i];
            if (itemUI == null || itemUI.RectTransform == null)
                continue;
 
            itemUI.gameObject.SetActive(true);
            itemUI.RectTransform.anchoredPosition = new Vector2(posX, 0);
 
            float absDist = Mathf.Abs(distanceToCenter);
            float scaleProgress = Mathf.Clamp01(1f - absDist);
            float currentScale = Mathf.Lerp(ScaleMin, ScaleMax, scaleProgress);
            itemUI.RectTransform.localScale = Vector3.one * currentScale;
 
            bool isSelected = logicIndex == selectedLogicIndex;
            RefreshItem(itemUI, dataList[realIndex], realIndex, isSelected);
            itemUI.SetSelected(isSelected);
        }
    }
 
    int GetRealIndex(int logicIndex)
    {
        return (logicIndex % dataList.Count + dataList.Count) % dataList.Count;
    }
 
    protected abstract void RefreshItem(TItem item, TData data, int realIndex, bool isSelected);
 
    public void OnBeginDrag(PointerEventData eventData)
    {
        isDragging = true;
    }
 
    public void OnDrag(PointerEventData eventData)
    {
        if (dataList.IsNullOrEmpty())
            return;
 
        float dragDeltaX = eventData.delta.x;
        currentCenterOffset -= dragDeltaX / ItemSpacing;
        UpdateItems();
    }
 
    public void OnEndDrag(PointerEventData eventData)
    {
        if (dataList.IsNullOrEmpty())
            return;
 
        isDragging = false;
 
        float speedX = eventData.delta.x;
        if (speedX > 5f)
        {
            targetCenterOffset = Mathf.Floor(currentCenterOffset);
        }
        else if (speedX < -5f)
        {
            targetCenterOffset = Mathf.Ceil(currentCenterOffset);
        }
        else
        {
            targetCenterOffset = Mathf.Round(currentCenterOffset);
        }
 
        int oldIndex = currentSelectedIndex;
        currentSelectedIndex = GetRealIndex(Mathf.RoundToInt(targetCenterOffset));
        UpdateItems();
 
        if (oldIndex != currentSelectedIndex)
            OnSelectedIndexChanged?.Invoke();
    }
}