1. 程式人生 > >【Unity】UGUI ScrollView 分頁 單次拖拽滑動一頁

【Unity】UGUI ScrollView 分頁 單次拖拽滑動一頁

一,新建ScrollView ,目錄結構如圖:

二,在content下編輯需要顯示的關卡內容,這裡設定為一頁顯示一個button集合,14個button為一整頁,一次只顯示一頁內容:

下面上程式碼:

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

public class MyScrollRectHelper : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{

    private float smooting;                          //滑動速度
    private float normalSpeed = 5;
    private float highSpeed = 100;
    private int pageCount = 1;                           //每頁顯示的專案
    public GameObject listItem;
    private ScrollRect sRect;
    private float pageIndex;                            //總頁數
    private bool isDrag = false;                                //是否拖拽結束
    private List<float> listPageValue = new List<float> { 0 };  //總頁數索引比列 0-1


    private float m_targetPos = 0;                                //滑動的目標位置

    private float nowindex = 0;                                 //當前位置索引
    private float beginDragPos;
    private float endDragPos;

    private float sensitivity = 0.15f;                          //靈敏度

    public Button nextPage;
    public Button prePage;
    private int onePageCount = 10;

    void Awake()
    {
        sRect = this.GetComponent<ScrollRect>();
        ListPageValueInit();
        smooting = normalSpeed;
        ButtonInit();
    }

    //每頁比例
    void ListPageValueInit()
    {
        pageIndex = (listItem.transform.childCount / pageCount) - 1;
        if (listItem != null && listItem.transform.childCount != 0)
        {
            for (float i = 1; i <= pageIndex; i++)
            {
                listPageValue.Add((i / pageIndex));
            }
        }
    }

    void ButtonInit()
    {
        nextPage.onClick.AddListener(BtnRightGo);
        prePage.onClick.AddListener(BtnLeftGo);
    }

    void Update()
    {
        if (!isDrag)
            sRect.horizontalNormalizedPosition = Mathf.Lerp(sRect.horizontalNormalizedPosition, m_targetPos, Time.deltaTime * smooting);
    }

    /// <summary>
    /// 拖動開始
    /// </summary>
    /// <param name="eventData"></param>
    public void OnBeginDrag(PointerEventData eventData)
    {
        isDrag = true;
        beginDragPos = sRect.horizontalNormalizedPosition;
    }

    /// <summary>
    /// 拖拽結束
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
        endDragPos = sRect.horizontalNormalizedPosition; //獲取拖動的值
        endDragPos = endDragPos > beginDragPos ? endDragPos + sensitivity : endDragPos - sensitivity;
        int index = 0;
        float offset = Mathf.Abs(listPageValue[index] - endDragPos);    //拖動的絕對值
        for (int i = 1; i < listPageValue.Count; i++)
        {
            float temp = Mathf.Abs(endDragPos - listPageValue[i]);
            if (temp < offset)
            {
                index = i;
                offset = temp;
            }
        }
        m_targetPos = listPageValue[index];
        nowindex = index;
    }

    public void BtnLeftGo()
    {
        nowindex = Mathf.Clamp(nowindex - 1, 0, pageIndex);
        m_targetPos = listPageValue[Convert.ToInt32(nowindex)];
    }

    public void BtnRightGo()
    {
        nowindex = Mathf.Clamp(nowindex + 1, 0, pageIndex);
        m_targetPos = listPageValue[Convert.ToInt32(nowindex)];
    }
}