Unity UGUI实现滑动翻页效果
更新时间:2020年6月25日 10:34 点击:2245
本文实例为大家分享了Unity UGUI实现滑动翻页效果的具体代码,供大家参考,具体内容如下
这个问题真的是老生常谈的事情了,不过在这里还是要说一下,以便以后之需
首先看一下效果图
最后在Content下面是一些Image
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; } }
最后看一下,怎么设置的:
剩下的就没有什么了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 这篇文章主要为大家详细介绍了jQuery实现下拉菜单滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
- 这篇文章主要为大家详细介绍了uniapp 实现可以左右滑动导航栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-10-21
- 这篇文章主要介绍了JavaScript 获取滚动条位置并将页面滑动到锚点的的相关资料,帮助大家更好的理解和学习使用JavaScript,感兴趣的朋友可以了解下...2021-02-09
- 这篇文章主要为大家详细介绍了Unity时间戳的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要为大家分享了js滑动提示效果代码,实现方法简单,感兴趣的小伙伴们可以参考一下...2016-03-12
- 本文给大家介绍利用Vue实现tab导航栏,并且通过flex布局实现左右滑动效果,通过代码给大家分享tab导航栏布局的实现,本文给大家展示了完整代码,需要的朋友参考下吧...2021-06-28
Unity中 ShaderGraph 实现旋涡传送门效果入门级教程(推荐)
通过Twirl 旋转节点对Gradient Noise 梯度噪声节点进行操作,就可得到一个旋转的旋涡效果。具体实现代码跟随小编一起通过本文学习下吧...2021-07-11- 本文主要介绍了4种延时执行的方法,主要包括Update计时器,Invoke,协程,DoTween,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-07
- 这篇文章主要为大家详细介绍了Unity shader实现遮罩效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了unity 判断鼠标是否在哪个UI上的两种实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-10
- 这篇文章主要为大家详细介绍了Unity实现换装系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-04-11
- 这篇文章主要给大家介绍了关于利用unity代码C#封装为dll的相关资料,文中通过图文将实现的方法介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要为大家详细介绍了Unity Shader实现径向模糊效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
- 这篇文章主要介绍了unity 实现摄像机绕某点旋转一周,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要为大家详细介绍了Unity实现截图功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
jQuery和hwSlider实现内容响应式可触控滑动切换效果附源码下载(二)
这篇文章主要介绍了jQuery和hwSlider实现内容响应式可触控滑动切换效果附源码下载(二)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-06-24- 这篇文章主要介绍了在vue中实现禁止屏幕滚动,禁止屏幕滑动,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-22
- 这篇文章主要介绍了Unity中EventTrigger的几种使用操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-10
- 这篇文章主要为大家详细介绍了Unity Shader实现2D水流效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要为大家详细介绍了JavaScript+html实现前端页面滑动验证,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-06-06