【Unity】UGUI ScrollView 分页 单次拖拽滑动一页

本文介绍了一种在Unity中实现水平滑动分页的方法,通过自定义MyScrollRectHelper类来控制UIScrollView的滑动行为,包括滑动速度、灵敏度等参数的设置,并提供了前后翻页按钮的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,新建ScrollView ,目录结构如图:

二,在content下编辑需要显示的关卡内容,这里设置为一页显示一个button集合,14个button为一整页,一次只显示一页内容:

下面上代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System;

public class MyScrollRectHelper : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{

    private float smooting;                          //滑动速度
    private float normalSpeed = 5;
    private float highSpeed = 100;
    private int pageCount = 1;                           //每页显示的项目
    public GameObject listItem;
    private ScrollRect sRect;
    private float pageIndex;                            //总页数
    private bool isDrag = false;                                //是否拖拽结束
    private List<float> listPageValue = new List<float> { 0 };  //总页数索引比列 0-1


    private float m_targetPos = 0;                                //滑动的目标位置

    private float nowindex = 0;                                 //当前位置索引
    private float beginDragPos;
    private float endDragPos;

    private float sensitivity = 0.15f;                          //灵敏度

    public Button nextPage;
    public Button prePage;
    private int onePageCount = 10;

    void Awake()
    {
        sRect = this.GetComponent<ScrollRect>();
        ListPageValueInit();
        smooting = normalSpeed;
        ButtonInit();
    }

    //每页比例
    void ListPageValueInit()
    {
        pageIndex = (listItem.transform.childCount / pageCount) - 1;
        if (listItem != null && listItem.transform.childCount != 0)
        {
            for (float i = 1; i <= pageIndex; i++)
            {
                listPageValue.Add((i / pageIndex));
            }
        }
    }

    void ButtonInit()
    {
        nextPage.onClick.AddListener(BtnRightGo);
        prePage.onClick.AddListener(BtnLeftGo);
    }

    void Update()
    {
        if (!isDrag)
            sRect.horizontalNormalizedPosition = Mathf.Lerp(sRect.horizontalNormalizedPosition, m_targetPos, Time.deltaTime * smooting);
    }

    /// <summary>
    /// 拖动开始
    /// </summary>
    /// <param name="eventData"></param>
    public void OnBeginDrag(PointerEventData eventData)
    {
        isDrag = true;
        beginDragPos = sRect.horizontalNormalizedPosition;
    }

    /// <summary>
    /// 拖拽结束
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
        endDragPos = sRect.horizontalNormalizedPosition; //获取拖动的值
        endDragPos = endDragPos > beginDragPos ? endDragPos + sensitivity : endDragPos - sensitivity;
        int index = 0;
        float offset = Mathf.Abs(listPageValue[index] - endDragPos);    //拖动的绝对值
        for (int i = 1; i < listPageValue.Count; i++)
        {
            float temp = Mathf.Abs(endDragPos - listPageValue[i]);
            if (temp < offset)
            {
                index = i;
                offset = temp;
            }
        }
        m_targetPos = listPageValue[index];
        nowindex = index;
    }

    public void BtnLeftGo()
    {
        nowindex = Mathf.Clamp(nowindex - 1, 0, pageIndex);
        m_targetPos = listPageValue[Convert.ToInt32(nowindex)];
    }

    public void BtnRightGo()
    {
        nowindex = Mathf.Clamp(nowindex + 1, 0, pageIndex);
        m_targetPos = listPageValue[Convert.ToInt32(nowindex)];
    }
}

 

Demo链接。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国家一级摸鱼选手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值