Unity ugui 学习记录(一) ScrollView实现分页滚动功能

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、ScrollView实现分页滚动功能

代码如下:

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


public enum PageScrollType {
    Horizontal,
    Vertical
}

public class PageScrollView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
    protected ScrollRect rect;
    protected int pageCount;
    private RectTransform content;
    protected float[] pages;

    public float moveTime = 0.3f;
    private float timer = 0;
    private float startMovePos;
    protected int currentPage = 0;

    private bool isDraging = false;

    private bool isMoving = false;
    // 是不是开启自动滚动
    public bool IsAutoScroll;
    public float AutoScrollTime = 2;
    private float AutoScrollTimer = 0;

    public PageScrollType pageScrollType = PageScrollType.Horizontal;

    public float vertical;

    public Action<int> OnPageChange;

    protected virtual void Start()
    {
        Init();
    }

    protected virtual void Update()
    {
        ListenerMove();
        ListenerAutoScroll();
        vertical = rect.verticalNormalizedPosition;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        // 滚动到离得最近的一页
        this.ScrollToPage(CaculateMinDistancePage());
        isDraging = false;
        AutoScrollTimer = 0;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        isDraging = true;
    }

    public void Init() {
        rect = transform.GetComponent<ScrollRect>();
        if (rect == null)
        {
            throw new System.Exception(" 未查询到ScrollRect! ");
        }
        content = transform.Find("Viewport/Content").GetComponent<RectTransform>();
        pageCount = content.childCount;
        if (pageCount == 1)
        {
            throw new System.Exception("只有一页,不用进行分页滚动!");
        }
        pages = new float[pageCount];
        for (int i = 0; i < pages.Length; i++)
        {
            switch (pageScrollType)
            {
                case PageScrollType.Horizontal:
                    pages[i] = i * (1.0f / (float)(pageCount - 1));
                    break;
                case PageScrollType.Vertical:
                    pages[i] = 1 - i * (1.0f / (float)(pageCount - 1));
                    break;
            }

        }

    }

    // 监听移动
    public void ListenerMove()
    {
        if (isMoving)
        {
            timer += Time.deltaTime * (1 / moveTime);
            switch (pageScrollType)
            {
                case PageScrollType.Horizontal:
                    rect.horizontalNormalizedPosition = Mathf.Lerp(startMovePos, pages[currentPage], timer);
                    break;
                case PageScrollType.Vertical:
                    rect.verticalNormalizedPosition = Mathf.Lerp(startMovePos, pages[currentPage], timer);
                    break;
            }
            if (timer >= 1)
            {
                isMoving = false;
            }
        }
    }
    // 监听自动滚动
    public void ListenerAutoScroll()
    {

        if (isDraging) { return; }

        if (IsAutoScroll)
        {
            AutoScrollTimer += Time.deltaTime;
            if (AutoScrollTimer >= AutoScrollTime)
            {
                AutoScrollTimer = 0;
                // 滚动到下一页
                currentPage++;
                currentPage %= pageCount;
                ScrollToPage(currentPage);
            }
        }
    }

    public void ScrollToPage(int page)
    {
        isMoving = true;
        this.currentPage = page;
        timer = 0;
        switch (pageScrollType)
        {
            case PageScrollType.Horizontal:
                startMovePos = rect.horizontalNormalizedPosition;
                break;
            case PageScrollType.Vertical:
                startMovePos = rect.verticalNormalizedPosition;
                break;
        }
        if (OnPageChange != null)
        {
            OnPageChange(this.currentPage);
        }
    }

    // 计算离得最近的一页
    public int CaculateMinDistancePage() {
        int minPage = 0;
        // 计算出离得最近的一页
        for (int i = 1; i < pages.Length; i++)
        {
            switch (pageScrollType)
            {
                case PageScrollType.Horizontal:

                    if (Mathf.Abs(pages[i] - rect.horizontalNormalizedPosition) < Mathf.Abs(pages[minPage] - rect.horizontalNormalizedPosition))
                    {
                        minPage = i;
                    }
                    break;
                case PageScrollType.Vertical:

                    if (Mathf.Abs(pages[i] - rect.verticalNormalizedPosition) < Mathf.Abs(pages[minPage] - rect.verticalNormalizedPosition))
                    {
                        minPage = i;
                    }
                    break;
            }
        }
        return minPage;
    }  
    #endregion
}

二、ScrollView实现分页滚动缩放功能

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScalePageScrollView : PageScrollView
{
    // 所有页的Object
    protected GameObject[] items;

    public float currentScale = 1f;
    public float otherScale = 0.6f;

    public int lastPage;
    public int nextPage;

    protected override void Start()
    {
        base.Start();
        items = new GameObject[pageCount];
        // 初始化所有的GameObject 
        for (int i = 0; i < pageCount; i++)
        {
            items[i] = transform.Find("Viewport/Content").GetChild(i).gameObject;
        }
    }

    protected override void Update()
    {
        base.Update();
        ListenerScale();
    }

    // 监听scale
    public void ListenerScale() {
        // 找到上一页 和 下一页

        for (int i = 0; i < pages.Length; i++)
        {
            if ( pages[i] <= rect.horizontalNormalizedPosition )
            {
                lastPage = i;
            }
        }

        for (int i = 0; i < pages.Length; i++)
        {
            if (pages[i] > rect.horizontalNormalizedPosition)
            {
                nextPage = i;
                break;
            }
        }

        if ( nextPage == lastPage )
        {
            return;
        }

        float percent = (rect.horizontalNormalizedPosition - pages[lastPage]) / ( pages[nextPage] - pages[lastPage] );
        items[lastPage].transform.localScale = Vector3.Lerp(Vector3.one * currentScale, Vector3.one * otherScale, percent);
        items[nextPage].transform.localScale = Vector3.Lerp(Vector3.one * currentScale, Vector3.one * otherScale, 1 - percent);

        for (int i = 0; i < items.Length; i++)
        {
            if (i != lastPage && i != nextPage)
            {
                items[i].transform.localScale = Vector3.one * otherScale;
            }
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值