Csharp/C#教程:Unity工具类ScrollView实现拖拽滑动翻页分享

简介:

在进行UI设计的时候,经常会使用Unity中UI提供的ScrollView,类似Android中的ScrollView,在进行图片预览,多个翻页的时候,能实现很好的效果。

该类中根据Unity的EventSystems中拖拽事件,实现对页码的滑动监听,在使用的时候,新建UI—>ScrollView,把该类组件添加到ScrollView上,把对应的content加入该脚本中的content,调整ScrollView和Content,设置单个滑动页的宽度,拖拽的阈值,即可监听到拖拽,如果是动态实例化ScrollView中的child,需设置当前最大页码数。SetCurIndex可以实现直接定位到当前页码对应的滑动页,代码比较简单,直接贴出来。

publicclassScrollViewListener:MonoBehaviour,IDragHandler,IBeginDragHandler,IEndDragHandler { //滑动方向 publicenumMoveDirection { None=0, Left, Right, } publicfloatSingleItemWidth;//单个滑动页的宽度 publicRectTransformcontent;//当前ScrollView的Content publicfloatDragMinValue=5f;//拖动过程中允许的最小的拖拽值,低于此值就不算拖拽,不执行翻页事件 privateMoveDirectiondirection=MoveDirection.None; privateintCurIndex=0;//当前页码 privateintMaxIndex=0;//最大页码 publicboolcanMove=true;//是否能移动 privateVector3originalPos; privatefloatmaxDeltaX=0f;//取整个拖动过程中的最大值 publicAction<int>OnPageChange;//对外提供页码修改的回调 ///<summary> ///滑到下一页 ///</summary> privatevoidMoveToNext() { 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; }); } } elseif(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> ///<paramname="max"></param> publicvoidSetMaxIndex(intmax) { MaxIndex=max-1;//最大下标值为页数减1 } ///<summary> ///设置当前页 ///</summary> ///<paramname="index"></param> publicvoidSetCurIndex(intindex) { CurIndex=index; floatx=content.localPosition.x-SingleItemWidth*CurIndex; content.localPosition=newVector3(x,content.localPosition.y,content.localPosition.z); } publicvoidResetPosition() { content.localPosition=originalPos; } privatevoidAwake() { CurIndex=0; originalPos=content.localPosition; } publicvoidOnDrag(PointerEventDataeventData) { if(Mathf.Abs(eventData.delta.x)>maxDeltaX) { maxDeltaX=Mathf.Abs(eventData.delta.x); } } publicvoidOnBeginDrag(PointerEventDataeventData) { if(eventData.delta.x>0) { direction=MoveDirection.Right; } elseif(eventData.delta.x<0) { direction=MoveDirection.Left; } else { direction=MoveDirection.None; } if(Mathf.Abs(eventData.delta.x)>maxDeltaX) { maxDeltaX=Mathf.Abs(eventData.delta.x); } } publicvoidOnEndDrag(PointerEventDataeventData) { if(Mathf.Abs(eventData.delta.x)>maxDeltaX) { maxDeltaX=Mathf.Abs(eventData.delta.x); } if(maxDeltaX>DragMinValue) { //计算下一页的目的点然后移动 if(canMove) { MoveToNext(); } } maxDeltaX=0f; } } 您可能感兴趣的文章:UnityUGUI实现滑动翻页直接跳转页数Unity使用ScrollRect制作翻页UnityUGUI实现滑动翻页效果unity实现翻页按钮功能unityscrollRect实现按页码翻页效果

标签: ie 工具 ni 工具类

使用 bind 设置 DNS 服务器的方法

Ubuntu18.04系统安装、配置Redis及phpredis扩展操作详解

上述就是C#学习教程:Unity工具类ScrollView实现拖拽滑动翻页分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/903954.html

(0)
上一篇 2021年10月21日
下一篇 2021年10月21日

精彩推荐