提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、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 &#