服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity UGUI实现滑动翻页效果

Unity UGUI实现滑动翻页效果

2022-09-02 11:51同灯花城 C#

这篇文章主要为大家详细介绍了Unity UGUI实现滑动翻页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了unity ugui实现滑动翻页效果的具体代码,供大家参考,具体内容如下

这个问题真的是老生常谈的事情了,不过在这里还是要说一下,以便以后之需

首先看一下效果图

Unity UGUI实现滑动翻页效果

Unity UGUI实现滑动翻页效果

Unity UGUI实现滑动翻页效果

Unity UGUI实现滑动翻页效果

Unity UGUI实现滑动翻页效果

最后在content下面是一些image

Unity UGUI实现滑动翻页效果

?
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;
 }
}

最后看一下,怎么设置的:

Unity UGUI实现滑动翻页效果

Unity UGUI实现滑动翻页效果

Unity UGUI实现滑动翻页效果

剩下的就没有什么了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_41814169/article/details/88890650

延伸 · 阅读

精彩推荐
  • C#C#中DataGridView动态添加行及添加列的方法

    C#中DataGridView动态添加行及添加列的方法

    这篇文章主要介绍了C#中DataGridView动态添加行及添加列的方法,涉及C#中DataGridView针对行与列动态操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考...

    我心依旧8312021-10-26
  • C#C#中WebClient实现文件下载

    C#中WebClient实现文件下载

    本篇文章主要介绍了C#中WebClient实现文件下载,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    sparkdev3582021-12-23
  • C#基于c#用Socket做一个局域网聊天工具

    基于c#用Socket做一个局域网聊天工具

    目前基于Internet的即时聊天工具已经做的非常完美,本文介绍了基于c#用Socket做一个局域网聊天工具,有需要的朋友可以看一下。...

    Create Chen10202021-12-08
  • C#C#读取XML的三种实现方式

    C#读取XML的三种实现方式

    XML文件是一种常用的文件格式,本篇文章主要介绍了C#读取XML的三种实现方式,主要是XmlDocument、XmlTextReader、Linq to Xml,有兴趣的可以了解一下。...

    WindyAmy11742021-12-23
  • C#c#删除指定文件夹中今天之前的文件

    c#删除指定文件夹中今天之前的文件

    本文主要介绍了c#删除指定文件夹中今天之前文件的方法,具有很好的参考价值,下面跟着小编一起来看下吧...

    冷战10622021-12-27
  • C#Unity向量按照某一点进行旋转

    Unity向量按照某一点进行旋转

    这篇文章主要为大家详细介绍了Unity向量按照某一点进行旋转,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    类人_猿3382022-08-17
  • C#Unity UGUI实现卡片椭圆方向滚动

    Unity UGUI实现卡片椭圆方向滚动

    这篇文章主要为大家详细介绍了UGUI实现卡片椭圆方向滚动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    尘世喧嚣7972022-07-09
  • C#C#实现多线程写入同一个文件的方法

    C#实现多线程写入同一个文件的方法

    这篇文章主要介绍了C#实现多线程写入同一个文件的方法,涉及C#多线程操作文件读写的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    我心依旧5922021-10-20