欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

unity scrollRect实现按页码翻页效果

程序员文章站 2022-05-26 10:05:25
本文实例为大家分享了unity实现按页码翻页效果的具体代码,供大家参考,具体内容如下用来做背包 商店的按页翻页功能,先上效果图其中,dragnum 表示的是 如果为3,便是滑动距离超过当前页面宽度的百...

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

用来做背包 商店的按页翻页功能,先上效果图

unity scrollRect实现按页码翻页效果unity scrollRect实现按页码翻页效果

其中,dragnum 表示的是 如果为3,便是滑动距离超过当前页面宽度的百分之三十位成功

connect表示 scrollrect下connet的大小

unity scrollRect实现按页码翻页效果

然后是函数的初始化方法 这里提供了两个方法  一个是直接提供页数 参数分别代表 总页数,要显示的页数 以及切换到要显示的页数是否播放滑动动画

以及当时用来做背包、商店的方法2

unity scrollRect实现按页码翻页效果

和上面不用的是传入的是物品的数量以及每页显示格子数量的vector2 是否需要改变connect的大小 (需要提前把connect的大小设置为一页的大小)以及打开时显示的页数

思路大致如下 继承 ibegindraghandler, ienddraghandler两个接口

在begin时将需要的两个参数 scrollneedmove 以及scrolltargetvalue 置位默认值

在end时计算

unity scrollRect实现按页码翻页效果

unity scrollRect实现按页码翻页效果

具体代码如下

using unityengine;
using system.collections;
using unityengine.ui;
using unityengine.eventsystems;
public class scrollpagetool : monobehaviour, ibegindraghandler, ienddraghandler
{

 public button pagelastbutton;
 public button pagenextbutton;
 public text pagenumtext;
 public gridlayoutgroup itemconnect;
 [serializefield]
 private bool buttonpageenable;
 private int m_nowpage;//从0开始
 private int m_pagecount;
 /// <summary>
 /// 滑动距离超过一页的 (m_dragnum*10)% 则滑动成功
 /// </summary>
 public int m_dragnum;
 private float m_pageareasize;
 private const float scroll_smooth_time = 0.2f;
 private const float scroll_move_speed = 1f;
 private float scrollmovespeed = 0f;
 private bool scrollneedmove = false;
 private float scrolltargetvalue;
 public scrollrect scrollrect;
 
 private bool isregistevent = false;
 
 public bool setbuttonstatus
 {
 set
 {
 buttonpageenable = value;
 pagelastbutton.interactable = buttonpageenable && pagelastbutton.interactable;
 pagenextbutton.interactable = buttonpageenable && pagenextbutton.interactable;
 }
 }
 public void initmanager(int _allitemnum, vector2 pageitemsize, bool isneedchangesize = true, int pagenum = 0,bool isshowanim=false)
 {
 registevent();
 int _pageitemnum = (int)(pageitemsize.x * pageitemsize.y);
 m_pagecount = (_allitemnum / _pageitemnum) + ((_allitemnum % _pageitemnum == 0) ? 0 : 1);
 m_pageareasize = 1f / (m_pagecount - 1);
 changepage(pagenum);
 if (isneedchangesize)
 itemconnect.getcomponent<recttransform>().sizedelta = new vector2((itemconnect.cellsize.x * pageitemsize.x + itemconnect.spacing.x * pageitemsize.x) * m_pagecount,
 itemconnect.padding.top + itemconnect.padding.bottom + itemconnect.cellsize.y * pageitemsize.y + (pageitemsize.y - 1) * itemconnect.spacing.y);
 }
 
 public void initmanager(int pagenum, int targetpage = 0, bool isshowanim = false)
 {
 registevent();
 m_pagecount = pagenum;
 changepage(targetpage, isshowanim);
 }
 
 private void registevent()
 {
 if (isregistevent)
 return;
 isregistevent = true;
 if (pagelastbutton != null)
 pagelastbutton.onclick.addlistener(delegate { paging(-1); });
 if (pagenextbutton != null)
 pagenextbutton.onclick.addlistener(delegate { paging(1); });
 }
 
 private void paging(int num)
 {
 //maxnum-1,从0开始
 num = (num < 0) ? -1 : 1;
 int temp = mathf.clamp(m_nowpage + num, 0, m_pagecount - 1);
 if (m_nowpage == temp)
 return;
 changepage(temp);
 }
 void update()
 {
 scrollcontrol();
 }
 
 public int getpagenum { get { return m_nowpage; } }
 //按页翻动
 private void scrollcontrol()
 {
 if (!scrollneedmove)
 return;
 if (mathf.abs(scrollrect.horizontalnormalizedposition - scrolltargetvalue) < 0.01f)
 {
 scrollrect.horizontalnormalizedposition = scrolltargetvalue;
 scrollneedmove = false;
 return;
 }
 scrollrect.horizontalnormalizedposition = mathf.smoothdamp(scrollrect.horizontalnormalizedposition, scrolltargetvalue, ref scrollmovespeed, scroll_smooth_time);
 }
 
 public void onbegindrag(pointereventdata eventdata)
 {
 scrollneedmove = false;
 scrolltargetvalue = 0;
 }
 public void onenddrag(pointereventdata eventdata)
 {
 int temppage = m_nowpage;
 
 int num=(((scrollrect.horizontalnormalizedposition - (m_nowpage * m_pageareasize))>=0)?1:-1);
 
 if(mathf.abs(scrollrect.horizontalnormalizedposition - (m_nowpage * m_pageareasize))>= (m_pageareasize / 10f) * m_dragnum)
 {
 temppage+=num;
 
 }
 changepage(temppage);
 }
 
 public void changepage(int pagenum, bool isshowanim = true)
 {
 if (pagenum >= m_pagecount)
 pagenum = m_pagecount - 1;
 if (pagenum < 0)
 pagenum = 0;
 
 m_nowpage = pagenum;
 changepagetext(pagenum);
 if (isshowanim)
 {
 scrolltargetvalue = m_nowpage * m_pageareasize;
 scrollneedmove = true;
 scrollmovespeed = 0;
 }
 else
 {
 scrollrect.horizontalnormalizedposition = m_nowpage * m_pageareasize;
 }
 changepagetext(m_nowpage);
 }
 
 public void changepagetext(int num)
 {
 int maxpageto0start = m_pagecount - 1;
 m_nowpage = mathf.clamp(num, 0, maxpageto0start);
 
 if (pagenumtext != null)
 pagenumtext.text = (m_nowpage + 1).tostring() + "/" + m_pagecount.tostring();
 
 //only one page
 if (maxpageto0start == 0)
 {
 scrollrect.enabled = false;
 pagelastbutton.interactable = false;
 pagenextbutton.interactable = false;
 return;
 }
 else
 {
 pagelastbutton.interactable = true;
 pagenextbutton.interactable = true;
 scrollrect.enabled = true;
 }
 setbuttonstatus = buttonpageenable;
 if (!buttonpageenable)
 return;
 
 if (m_nowpage == 0 && pagelastbutton.interactable)
 pagelastbutton.interactable = false;
 if (m_nowpage >= maxpageto0start && pagenextbutton.interactable)
 pagenextbutton.interactable = false;
 if (m_nowpage > 0 && (!pagelastbutton.interactable))
 pagelastbutton.interactable = true;
 if (m_nowpage < maxpageto0start && (!pagenextbutton.interactable))
 pagenextbutton.interactable = true;
 
 
 }
}

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