首页 > 编程语言 > Unity ScrollView实现自动吸附效果
2021
11-16

Unity ScrollView实现自动吸附效果

本文实例为大家分享了Unity ScrollView实现自动吸附效果的具体代码,供大家参考,具体内容如下

一、效果演示

二、实现思路

通过使用UGUI的拖拽接口,在拖拽结束时比较当前滑动框的NormalizedPositon与每一页的NormalizedPositon值,找到距离当前拖拽结束位置最近的页并缓慢滑动过去

三、使用说明

——此功能脚本是对ScrollView的扩展,所以必须添加UGUI提供的基础Scroll View
——Content上必须添加GridLayoutGroup组件并添加所有列表中的项(不是动态添加),只是为了方便满足布局需求(我在代码中对startCorner、startAxis、childAlignment和constraintCount进行了限制,不需要对其设置)
——不能添加Content Size Fitter组件
——测试出适合的视为滑动一页的距离和视为滑动多页的距离数值并填入即可

四、完整代码

将AutoAdsorbScrollView脚本挂载到ScrollView上

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
/// <summary>
/// 自动吸附的滑动列表
/// </summary>
public class AutoAdsorbScrollView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
    private ScrollRect scrollRect;//滑动框组件
    private RectTransform content;//滑动框的Content
    private GridLayoutGroup layout;//布局组件
 
    private int totalPage; //总页数
    private int curPage; //当前页的下标
    private float[] eachPageNUPos; //每页的NormalizedPositon的值
    private float targetNUPos; //目标页的NormalizedPositon的值
 
    private Vector2 beginMousePos; //鼠标开始按下的位置
    private Vector2 endMousePos; //鼠标结束按下的位置
    private bool isDrag; //是否在拖拽
 
    [Header("是否可以滑动多页")]
    public bool sliderMultPage;
 
    [Header("视为滑动一页的距离")]
    [Space(25)]
    public float sliderOnePageDis;
    [Header("视为滑动多页的距离")]
    public float sliderMultPageDis;
    [Header("缓动到目标页的持续时间")]
    public float duration;
 
    #region Init
 
    private void Awake()
    {
        scrollRect = GetComponent<ScrollRect>();
        content = scrollRect.content;
        layout = content.GetComponent<GridLayoutGroup>();
 
        Init();//初始化
    }
 
    /// <summary>
    /// 初始化
    /// </summary>
    private void Init()
    {
        totalPage = content.childCount;
 
        SetContentSize();//设置Content大小
 
        CalcEachPageNUPos();//计算每一页的NormalizedPositon值
 
        SetLayout();//设置布局
    }
 
    /// <summary>
    /// 设置Content大小
    /// </summary>
    private void SetContentSize()
    {
        content.sizeDelta = new Vector2
            (
                layout.padding.right + layout.padding.left + (totalPage - 1) * (layout.cellSize.x + layout.spacing.x) - layout.spacing.x,
                content.sizeDelta.y
            ); ;
    }
 
    /// <summary>
    /// 计算每一页的NormalizedPositon值
    /// </summary>
    private void CalcEachPageNUPos()
    {
        float tempNUPos = 0;
        eachPageNUPos = new float[totalPage];
        for (int i = 0; i < totalPage; i++)
        {
            eachPageNUPos[i] = tempNUPos;
            tempNUPos += 1f / (totalPage - 1);
        }
    }
 
    /// <summary>
    /// 设置布局
    /// </summary>
    private void SetLayout()
    {
        scrollRect.horizontal = true;
        scrollRect.vertical = false;
        layout.padding.right = layout.padding.left;
        layout.startCorner = GridLayoutGroup.Corner.UpperLeft;
        layout.childAlignment = TextAnchor.MiddleCenter;
        layout.constraintCount = 1;
    }
 
    #endregion
 
    #region Main
 
    /// <summary>
    /// 拖拽开始
    /// </summary>
    public void OnBeginDrag(PointerEventData eventData)
    {
        isDrag = true;
        beginMousePos = Input.mousePosition;
    }
 
    /// <summary>
    /// 拖拽结束
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
        coe = 0;
 
        endMousePos = Input.mousePosition;
        Vector2 offset = endMousePos - beginMousePos;
        Debug.Log("滑动距离为:" + offset);
 
        if (sliderMultPage)
        {
            //单页滑动
            if (Mathf.Abs(offset.x) >= sliderOnePageDis && Mathf.Abs(offset.x) < sliderMultPageDis)
            {
                float tempHorizontalNUPos = scrollRect.horizontalNormalizedPosition;
                FindNearlyPage(tempHorizontalNUPos);
            }
            //多页滑动
            else if (Mathf.Abs(offset.x) >= sliderMultPageDis)
            {
                if (offset.x > 0)
                {
                    curPage = 0;
                }
                else if (offset.x < 0)
                {
                    curPage = totalPage - 1;
                }
            }
        }
        else
        {
            //单页滑动
            if (Mathf.Abs(offset.x) >= sliderOnePageDis)
            {
                float tempHorizontalNUPos = scrollRect.horizontalNormalizedPosition;
                FindNearlyPage(tempHorizontalNUPos);
            }
        }
 
        targetNUPos = eachPageNUPos[curPage];
    }
 
    private float coe;//比例系数
    private void Update()
    {
        if (isDrag)
        {
            return;
        }
        coe += Time.deltaTime / duration;
        scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, targetNUPos, coe);
    }
 
    #endregion
 
    #region Tool
 
    /// <summary>
    /// 寻找距离当前NormalizedPositon最近的页
    /// </summary>
    private void FindNearlyPage(float tempHorizontalNUPos)
    {
        float minOffset = Mathf.Abs(eachPageNUPos[0] - tempHorizontalNUPos);
        for (int i = 0; i < totalPage; i++)
        {
            float tempHorizontalOffset = Mathf.Abs(eachPageNUPos[i] - tempHorizontalNUPos);
            if (tempHorizontalOffset <= minOffset)
            {
                minOffset = tempHorizontalOffset;
                curPage = i;
            }
        }
    }
 
    #endregion
}

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

编程技巧