简介:
在进行UI设计的时候,经常会使用Unity中UI提供的ScrollView,类似Android中的ScrollView,在进行图片预览,多个翻页的时候,能实现很好的效果。
该类中根据Unity的EventSystems中拖拽事件,实现对页码的滑动监听,在使用的时候,新建UI--->ScrollView,把该类组件添加到ScrollView上,把对应的content加入该脚本中的content,调整ScrollView和Content,设置单个滑动页的宽度,拖拽的阈值,即可监听到拖拽,如果是动态实例化ScrollView中的child,需设置当前最大页码数。SetCurIndex可以实现直接定位到当前页码对应的滑动页,代码比较简单,直接贴出来。
复制代码
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
135public class ScrollViewListener : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler { //滑动方向 public enum MoveDirection { None = 0, Left, Right, } public float SingleItemWidth;//单个滑动页的宽度 public RectTransform content;//当前ScrollView的Content public float DragMinValue = 5f;//拖动过程中允许的最小的拖拽值,低于此值就不算拖拽,不执行翻页事件 private MoveDirection direction = MoveDirection.None; private int CurIndex = 0;//当前页码 private int MaxIndex = 0;//最大页码 public bool canMove = true;//是否能移动 private Vector3 originalPos; private float maxDeltaX = 0f;//取整个拖动过程中的最大值 public Action<int> OnPageChange;//对外提供页码修改的回调 /// <summary> /// 滑到下一页 /// </summary> private void MoveToNext() { if (direction == MoveDirection.Left) { if (CurIndex < MaxIndex) { CurIndex++; canMove = false; content.DOLocalMoveX(content.localPosition.x - SingleItemWidth, 1f).OnComplete(() => { if (null != OnPageChange) { OnPageChange(CurIndex); } canMove = true; }); } } else if (direction == MoveDirection.Right) { if (CurIndex > 0) { CurIndex--; canMove = false; content.DOLocalMoveX(content.localPosition.x + SingleItemWidth, 1f).OnComplete(() => { if (null != OnPageChange) { OnPageChange(CurIndex); } canMove = true; }); } } } /// <summary> /// 设置当前滑动列表的页数的最大值 /// </summary> /// <param name="max"></param> public void SetMaxIndex(int max) { MaxIndex = max - 1;//最大下标值为页数减1 } /// <summary> /// 设置当前页 /// </summary> /// <param name="index"></param> public void SetCurIndex(int index) { CurIndex = index; float x = content.localPosition.x - SingleItemWidth * CurIndex; content.localPosition = new Vector3(x, content.localPosition.y, content.localPosition.z); } public void ResetPosition() { content.localPosition = originalPos; } private void Awake() { CurIndex = 0; originalPos = content.localPosition; } public void OnDrag(PointerEventData eventData) { if (Mathf.Abs(eventData.delta.x) > maxDeltaX) { maxDeltaX = Mathf.Abs(eventData.delta.x); } } public void OnBeginDrag(PointerEventData eventData) { if (eventData.delta.x > 0) { direction = MoveDirection.Right; } else if (eventData.delta.x < 0) { direction = MoveDirection.Left; } else { direction = MoveDirection.None; } if (Mathf.Abs(eventData.delta.x) > maxDeltaX) { maxDeltaX = Mathf.Abs(eventData.delta.x); } } public void OnEndDrag(PointerEventData eventData) { if (Mathf.Abs(eventData.delta.x) > maxDeltaX) { maxDeltaX = Mathf.Abs(eventData.delta.x); } if (maxDeltaX > DragMinValue) { //计算下一页的目的点 然后移动 if (canMove) { MoveToNext(); } } maxDeltaX = 0f; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是跳跃黑猫最近收集整理的关于Unity工具类ScrollView实现拖拽滑动翻页的全部内容,更多相关Unity工具类ScrollView实现拖拽滑动翻页内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复