Unity中改变horizontalNormalizedPosition值从而实现页面切换效果

首先在Unity中新建一个脚本SlideCanCoverScrollView,把这个脚本绑定在拥有ScrollView组件的游戏物体上面。然后复制以下的代码:

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

public class SlideCanCoverScrollView : MonoBehaviour,IBeginDragHandler,IEndDragHandler
{
    
    
    private float contentLength;//容器长度
    private float beginMousePositionX; //鼠标开始拖拽的X值
    private float endMousePositionX;  //鼠标结束拖拽的X值
    private ScrollRect scrollRect;   //绑定这个脚本上自身游戏物体上的的ScrollRect组件
    private float lastProportion;//上一个位置的比例

    public int cellLength;//每个单元格的长度(这里的长度指的是Content下的单个子元素的长度)
    public int spacing;//间隔(指的是两个子元素之间的间距)
    public int leftOffset;//左偏移量(指的是页面最左边距离屏幕最左边的距离)
    private float upperLimit;//上限值
    private float lowerLimit;//下限值
    private float firstItemLength;//移动第一个单元格的距离
    private float oneItemLength;//滑动一个单元格需要的距离
    private float oneItemProportion;//滑动一个单元格所占的比例

    public int totalItemNum;//共有几个单元格(在这里单元格其实指的就是Content下的元素个数)
    private int currentIndex;//当前单元格的索引

    private void Awake()
    {
    
    
        scrollRect = GetComponent<ScrollRect>();
        contentLength = scrollRect.content.rect.xMax;
        firstItemLength = cellLength / 2 + leftOffset;
        oneItemLength = cellLength + spacing;
        oneItemProportion = oneItemLength / contentLength;
        upperLimit = 1 - firstItemLength / contentLength;
        lowerLimit = firstItemLength / contentLength;
        currentIndex = 1;
        scrollRect.horizontalNormalizedPosition = 0;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
    
    
        beginMousePositionX = Input.mousePosition.x;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
    
    
        float offSetX = 0;
        endMousePositionX = Input.mousePosition.x;
        offSetX = (beginMousePositionX - endMousePositionX) * 2;
        //Debug.Log("offSetX:" + offSetX);
        //Debug.Log("firstItemLength:" + firstItemLength);
        if (Mathf.Abs(offSetX) > firstItemLength)//执行滑动动作的前提是要大于第一个需要滑动的距离
        {
    
    
            if (offSetX > 0)//右滑
            {
    
    
                if (currentIndex >= totalItemNum)
                {
    
    
                    return;
                }
                int moveCount =
                    (int)((offSetX - firstItemLength) / oneItemLength) + 1;//当次可以移动的格子数目
                currentIndex += moveCount;
                if (currentIndex >= totalItemNum)
                {
    
    
                    currentIndex = totalItemNum;
                }
                //当次需要移动的比例:上一次已经存在的单元格位置
                //的比例加上这一次需要去移动的比例
                lastProportion += oneItemProportion * moveCount;
                if (lastProportion >= upperLimit)
                {
    
    
                    lastProportion = 1;
                }
            }
            else //左滑
            {
    
    
                if (currentIndex <= 1)
                {
    
    
                    return;
                }
                int moveCount =
                    (int)((offSetX + firstItemLength) / oneItemLength) - 1;//当次可以移动的格子数目
                currentIndex += moveCount;
                if (currentIndex <= 1)
                {
    
    
                    currentIndex = 1;
                }
                //当次需要移动的比例:上一次已经存在的单元格位置
                //的比例加上这一次需要去移动的比例
                lastProportion += oneItemProportion * moveCount;
                if (lastProportion <= lowerLimit)
                {
    
    
                    lastProportion = 0;
                }
            }
        }
        DOTween.To(() => scrollRect.horizontalNormalizedPosition,
            lerpValue => scrollRect.horizontalNormalizedPosition = lerpValue,
            lastProportion, 0.5f).SetEase(Ease.OutQuint);
    }
}


保存,然后回到Unity中等到代码编译完成后,就可以对我们在代码中公开的变量根据我注释的意思进行赋值。(在这里一定要注意的是要把Scroll View这个组件下的Content的Anchor Presets(锚点定位),在按下Alt键后选择最右下角的那一个!)

猜你喜欢

转载自blog.csdn.net/jianjianshini/article/details/112195672