当前位置: 移动技术网 > IT编程>开发语言>c# > Unity UGUI实现滑动翻页效果

Unity UGUI实现滑动翻页效果

2020年06月23日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了unity ugui实现滑动翻页效果的具体代码,供大家参考,具体内容如下这个问题真的是老生常谈的事情了,不过在这里还是要说一下,以便以后之需首先看一下效果图最后在content下面

本文实例为大家分享了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;
 } 
}

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

剩下的就没有什么了。

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网