Unity使用ScrollRect制作翻页

所属分类: 软件编程 / C#教程 阅读数: 47
收藏 0 赞 0 分享

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

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

更多精彩内容其他人还在看

带你复习c# 托管和非托管资源

这篇文章主要介绍了c# 托管和非托管资源的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

C# lambda表达式原理定义及实例详解

这篇文章主要介绍了C# lambda表达式原理定义及实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#关于Task.Yeild()函数的讨论

这篇文章主要介绍了C#中关于Task.Yeild()函数的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

实例代码讲解c# 线程(上)

这篇文章主要介绍了讲解c# 线程的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

实例代码讲解c# 线程(下)

这篇文章主要介绍了c# 线程的的相关资料,文中示例代码非常细致,对大家的学习有很大帮助,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

c# rsa加密解密详解

这篇文章主要介绍了c# rsa加密解密的的相关资料,文中代码非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

c#中利用Tu Share获取股票交易信息

这篇文章主要介绍了c#中利用Tu Share获取股票交易信息,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C# MVC 使用LayUI实现下拉框二级联动的功能

这篇文章主要介绍了C# MVC 如何使用LayUI实现下拉框二级联动,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

浅谈C# 字段和属性

这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

浅谈C# 构造方法(函数)

这篇文章主要介绍了C# 构造方法(函数)的的相关资料,文中讲解非常详细,帮助大家更好的学习C#,感兴趣的朋友可以了解下
收藏 0 赞 0 分享
查看更多