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

Unity UGUI实现滑动翻页直接跳转页数

程序员文章站 2022-06-03 15:13:21
本文实例为大家分享了unity ugui实现滑动翻页,直接跳转页数的具体代码,供大家参考,具体内容如下首先看一下最终效果其实这个功能基本上是老生常谈了,所以代码还是很简单using unityengi...

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

首先看一下最终效果

Unity UGUI实现滑动翻页直接跳转页数

Unity UGUI实现滑动翻页直接跳转页数

其实这个功能基本上是老生常谈了,所以代码还是很简单

using unityengine;
using system.collections;
using unityengine.ui;
using system.collections.generic;
using unityengine.eventsystems;
using system;
 
public class pageview : monobehaviour, ibegindraghandler, ienddraghandler
{
 
 private scrollrect rect;      //滑动组件 
 private float targethorizontal = 0;    //滑动的起始坐标 
 private bool isdrag = false;     //是否拖拽结束 
 private list<float> poslist = new list<float>();   //求出每页的临界角,页索引从0开始 
 private int currentpageindex = -1;
 public action<int> onpagechanged;
 public recttransform content;
 private bool stopmove = true;
 public float smooting = 4;  //滑动速度 
 public float sensitivity = 0;
 private float starttime;
 
 private float startdraghorizontal;
 public transform togglelist;
 
 void start()
 {
  rect = transform.getcomponent<scrollrect>();
  var _rectwidth = getcomponent<recttransform>();
  var tempwidth = ((float)content.transform.childcount * _rectwidth.rect.width);
  content.sizedelta = new vector2(tempwidth, _rectwidth.rect.height);
  //未显示的长度
  float horizontallength = content.rect.width - _rectwidth.rect.width;
  for (int i = 0; i < rect.content.transform.childcount; i++)
  {
   poslist.add(_rectwidth.rect.width * i / horizontallength);
  }
 }
 
 void update()
 {
  if (!isdrag && !stopmove)
  {
   starttime += time.deltatime;
   float t = starttime * smooting;
   rect.horizontalnormalizedposition = mathf.lerp(rect.horizontalnormalizedposition, targethorizontal, t);
   if (t >= 1)
    stopmove = true;
  }
  //debug.log(rect.horizontalnormalizedposition);
 }
 
 public void pageto(int index)
 {
  if (index >= 0 && index < poslist.count)
  {
   rect.horizontalnormalizedposition = poslist[index];
   setpageindex(index);
   getindex(index);
  }
 }
 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;
 }
 
 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);
  //debug.log("offset " + offset);
 
 
  for (int i = 1; i < poslist.count; i++)
  {
   float temp = mathf.abs(poslist[i] - posx);
   //debug.log("temp " + temp);
   //debug.log("i" + i);
   if (temp < offset)
   {
    index = i;
    offset = temp;
   }
   //debug.log("index " + index);
  }
  //debug.log(index);
  setpageindex(index);
  getindex(index);
  targethorizontal = poslist[index]; //设置当前坐标,更新函数进行插值 
  isdrag = false;
  starttime = 0;
  stopmove = false;
 
 }
 
 public void getindex(int index)
 {
  var toogle = togglelist.getchild(index).getcomponent<toggle>();
  toogle.ison = true;
 }
}
using unityengine;
using system.collections;
using unityengine.ui;
using system;
 
public class gamecontroller : monobehaviour {
 [serializefield]
 private text pagenumber;
 [serializefield]
 private inputfield inputfield;
 [serializefield]
 private pageview pageview;
 // use this for initialization
 void start () {
  pagenumber.text = string.format ("当前页码:0");
  pageview.onpagechanged = pagechanged;
 }
 
 void pagechanged (int index) {
  pagenumber.text = string.format ("当前页码:{0}" , index.tostring ());
 }
 
 public void onclick () {
  try {
   int idnex = int.parse (inputfield.text);
   pageview.pageto (idnex);
  } catch(exception ex) {
   debug.logwarning ("请输入数字"+ex.tostring()); 
  }
 }
 
 void destroy () {
  pageview.onpagechanged = null;
 }
}

附上项目:地址

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