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

Unity使用ScrollRect制作翻页

程序员文章站 2023-11-14 14:45:40
本文实例为大家分享了使用scrollrect制作翻页的具体代码,供大家参考,具体内容如下1.标准的层级结构 scrollrect->viewport->content,viewport负责...

本文实例为大家分享了使用scrollrect制作翻页的具体代码,供大家参考,具体内容如下

1.标准的层级结构 scrollrect->viewport->content,viewport负责显示区域的大小一般和mask一起配合使用,content使用layout来布局,如果想使用代码来自动定位显示位置需要在content加上content size filter.

2.scrollrecthelper

using unityengine;
using unityengine.ui;
using unityengine.eventsystems;
using system.collections.generic;
using system;

public class scrollrecthelper : monobehaviour, ibegindraghandler, ienddraghandler
{
  // 滑动速度
  public float smooting = 5;

  // 每页显示的项目
  [serializefield]
  private int countperpage = 10;

  scrollrect srect;
  // 总页数
  float totalpages;
  // 是否拖拽结束
  bool isdrag = false;
  // 总页数索引比列 0-1
  list<float> listpagevalue = new list<float> { 0 };
  // 滑动的目标位置
  public float targetpos = 0;
  // 当前位置索引
  float nowindex = 0;                 

  void awake()
  {
    srect = getcomponent<scrollrect>();
  }

  public string pagetext()
  {
    return (nowindex + 1) + "/" + (totalpages + 1);
  }

  // 计算每页比例
  public void calclistpagevalue<t>() where t : monobehaviour
  {
    t[] items = srect.content.getcomponentsinchildren<t>();
    srect.content.rect.set(srect.content.rect.width / 2, srect.content.rect.y, srect.content.rect.width, srect.content.rect.height);
    totalpages = (int)(math.ceiling((float)items.length / countperpage) - 1);
    if (items.length != 0)
    {
      for (float i = 1; i <= totalpages; i++)
      {
        //debug.log(i / totalpages);
        listpagevalue.add((i / totalpages));
      }
    }
  }

  void update()
  {
    if (!isdrag)
    {
      srect.horizontalnormalizedposition = mathf.lerp(srect.horizontalnormalizedposition, targetpos,
        time.deltatime * smooting);
    }

    // debug
    if (input.getkeydown(keycode.leftarrow)) pressleft();
    if (input.getkeydown(keycode.rightarrow)) pressright();
  }

  /// <summary>
  /// 拖动开始
  /// </summary>
  /// <param name="eventdata"></param>
  public void onbegindrag(pointereventdata eventdata)
  {
    isdrag = true;
  }

  /// <summary>
  /// 拖拽结束
  /// </summary>
  /// <param name="eventdata"></param>
  public void onenddrag(pointereventdata eventdata)
  {
    isdrag = false;
    var temppos = srect.horizontalnormalizedposition; //获取拖动的值
    var index = 0;
    float offset = mathf.abs(listpagevalue[index] - temppos);  //拖动的绝对值
    for (int i = 1; i < listpagevalue.count; i++)
    {
      float temp = mathf.abs(temppos - listpagevalue[i]);
      if (temp < offset)
      {
        index = i;
        offset = temp;
      }
    }
    targetpos = listpagevalue[index];
    nowindex = index;
  }

  public void pressleft()
  {
    nowindex = mathf.clamp(nowindex - 1, 0, totalpages);
    targetpos = listpagevalue[convert.toint32(nowindex)];
  }

  public void pressright()
  {
    nowindex = mathf.clamp(nowindex + 1, 0, totalpages);
    targetpos = listpagevalue[convert.toint32(nowindex)];
  }
}

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