本文实例为大家分享了unity ugui实现滑动翻页效果的具体代码,供大家参考,具体内容如下
这个问题真的是老生常谈的事情了,不过在这里还是要说一下,以便以后之需
首先看一下效果图
最后在content下面是一些image
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
|
using unityengine; using system.collections; using unityengine.ui; using system.collections.generic; using unityengine.eventsystems; using system; public class pageview : monobehaviour, ibegindraghandler, ienddraghandler { scrollrect rect; //滑动组件 //public scrollrect rect2; //滑动组件2 private float targethorizontal = 0; //滑动的起始坐标 private bool isdrag = false ; //是否拖拽结束 private list< float > poslist = new list< float > (); //求出每页的临界角,页索引从0开始 private int currentpageindex = -1; public action< int > onpagechanged; private bool stopmove = true ; public float smooting = 4; //滑动速度 public float sensitivity = 0; private float starttime; private float startdraghorizontal; void awake () { // rect = rect2; rect = transform.getcomponent<scrollrect> (); // rect2 = transform.getcomponent<scrollrect>(); float horizontallength = rect.content.rect.width - getcomponent<recttransform> ().rect.width; //float horizontallength2 = rect2.content.rect.width - getcomponent<recttransform>().rect.width; poslist.add (0); for ( int i = 1; i < rect.content.transform.childcount - 1; i++) { poslist.add (getcomponent<recttransform> ().rect.width * i / horizontallength); } poslist.add (1); } void update () { if (!isdrag && !stopmove) { starttime += time.deltatime; float t = starttime * smooting; rect.horizontalnormalizedposition = mathf.lerp (rect.horizontalnormalizedposition , targethorizontal , t); // rect2.horizontalnormalizedposition = mathf.lerp(rect2.horizontalnormalizedposition, targethorizontal, t); if (t >= 1) stopmove = true ; } } public void pageto ( int index) { if (index >= 0 && index < poslist.count) { rect.horizontalnormalizedposition = poslist[index]; setpageindex(index); } else { debug.logwarning ( "页码不存在" ); } } private void setpageindex ( int index) { if (currentpageindex != index) { currentpageindex = index; if (onpagechanged != null ) onpagechanged (index); } } public void onbegindrag (pointereventdata eventdata) { isdrag = true ; startdraghorizontal = rect.horizontalnormalizedposition; // startdraghorizontal = rect2.horizontalnormalizedposition; } public void onenddrag (pointereventdata eventdata) { float posx = rect.horizontalnormalizedposition; posx += ((posx - startdraghorizontal) * sensitivity); posx = posx < 1 ? posx : 1; posx = posx > 0 ? posx : 0; int index = 0; float offset = mathf.abs (poslist[index] - posx); for ( int i = 1; i < poslist.count; i++) { float temp = mathf.abs (poslist[i] - posx); if (temp < offset) { index = i; offset = temp; } } setpageindex (index); targethorizontal = poslist[index]; //设置当前坐标,更新函数进行插值 isdrag = false ; starttime = 0; stopmove = false ; } } |
最后看一下,怎么设置的:
剩下的就没有什么了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_41814169/article/details/88890650