Unity PageView的效果

本文分享了一个Unity中自定义的滑动页面视图组件PageView,该组件能够实现流畅的手指拖拽翻页效果,并提供了精确的翻页位置控制。通过一系列参数配置可以调整翻页的灵敏度和速度。

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

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;                  //当前所在的index
    public Action<int> OnPageChanged;                   //page改变的回调函数

    private bool stopMove = true;
    public float smooting = 4;                          //滑动速度  
    public float sensitivity = 0;                       //翻页的灵敏度
    private float startTime;

    private float startDragHorizontal;                  //开始拖拽的Horizontal


    //-----------------------------------------------------------------------------------
    public void pageTo(int index)
    {
        if (index >= 0 && index < posList.Count)
        {
            rect.horizontalNormalizedPosition = posList[index];
            SetPageIndex(index);
        }
        else
        {
            Debug.LogWarning("页码不存在");
        }
    }


    //-----------------------------------------------------------------------------------
    void Awake () {
        rect = transform.GetComponent<ScrollRect> ();
        float horizontalLength = rect.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);//线性的返回到目标点
            if(t >= 1)
                stopMove = true;
        }
    }

    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) {
        //确定停留的index
        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;
    } 
}

主要是现在unity中很多基础的组件都需要自己再写一遍很累就出来共享下。当然这个是有参考大神的博客的。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值