1. 程式人生 > 程式設計 >Unity UGUI實現滑動翻頁直接跳轉頁數

Unity UGUI實現滑動翻頁直接跳轉頁數

本文例項為大家分享了Unity UGUI實現滑動翻頁,直接跳轉頁數的具體程式碼,供大家參考,具體內容如下

首先看一下最終效果

Unity UGUI實現滑動翻頁直接跳轉頁數

Unity UGUI實現滑動翻頁直接跳轉頁數

其實這個功能基本上是老生常談了,所以程式碼還是很簡單

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using System;
 
public class PageView : MonoBehaviour,IBeginDragHandler,IEndDragHandler
{
 
  private ScrollRect rect;            //滑動元件 
  private float targethorizontal = 0;       //滑動的起始座標 
  private bool isDrag = false;          //是否拖拽結束 
  private List<float> posList = new List<float>();      //求出每頁的臨界角,頁索引從0開始 
  private int currentPageIndex = -1;
  public Action<int> OnPageChanged;
  public RectTransform content;
  private bool stopMove = true;
  public float smooting = 4;   //滑動速度 
  public float sensitivity = 0;
  private float startTime;
 
  private float startDragHorizontal;
  public Transform toggleList;
 
  void Start()
  {
    rect = transform.GetComponent<ScrollRect>();
    var _rectWidth = GetComponent<RectTransform>();
    var tempWidth = ((float)content.transform.childCount * _rectWidth.rect.width);
    content.sizeDelta = new Vector2(tempWidth,_rectWidth.rect.height);
    //未顯示的長度
    float horizontalLength = content.rect.width - _rectWidth.rect.width;
    for (int i = 0; i < rect.content.transform.childCount; i++)
    {
      posList.Add(_rectWidth.rect.width * i / horizontalLength);
    }
  }
 
  void Update()
  {
    if (!isDrag && !stopMove)
    {
      startTime += Time.deltaTime;
      float t = startTime * smooting;
      rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition,targethorizontal,t);
      if (t >= 1)
        stopMove = true;
    }
    //Debug.Log(rect.horizontalNormalizedPosition);
  }
 
  public void pageTo(int index)
  {
    if (index >= 0 && index < posList.Count)
    {
      rect.horizontalNormalizedPosition = posList[index];
      SetPageIndex(index);
      GetIndex(index);
    }
  }
  private void SetPageIndex(int index)
  {
    if (currentPageIndex != index)
    {
      currentPageIndex = index;
      if (OnPageChanged != null)
        OnPageChanged(index);
    }
  }
 
  public void OnBeginDrag(PointerEventData eventData)
  {
    isDrag = true;
    //開始拖動
    startDragHorizontal = rect.horizontalNormalizedPosition;
  }
 
  public void OnEndDrag(PointerEventData eventData)
  {
    float posX = rect.horizontalNormalizedPosition;
    posX += ((posX - startDragHorizontal) * sensitivity);
    posX = posX < 1 ? posX : 1;
    posX = posX > 0 ? posX : 0;
    int index = 0;
 
    float offset = Mathf.Abs(posList[index] - posX);
    //Debug.Log("offset " + offset);
 
 
    for (int i = 1; i < posList.Count; i++)
    {
      float temp = Mathf.Abs(posList[i] - posX);
      //Debug.Log("temp " + temp);
      //Debug.Log("i" + i);
      if (temp < offset)
      {
        index = i;
        offset = temp;
      }
      //Debug.Log("index " + index);
    }
    //Debug.Log(index);
    SetPageIndex(index);
    GetIndex(index);
    targethorizontal = posList[index]; //設定當前座標,更新函式進行插值 
    isDrag = false;
    startTime = 0;
    stopMove = false;
 
  }
 
  public void GetIndex(int index)
  {
    var toogle = toggleList.GetChild(index).GetComponent<Toggle>();
    toogle.isOn = true;
  }
}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
 
public class GameController : MonoBehaviour {
  [SerializeField]
  private Text pageNumber;
  [SerializeField]
  private InputField inputField;
  [SerializeField]
  private PageView pageView;
 // Use this for initialization
 void Start () {
    pageNumber.text = string.Format ("當前頁碼:0");
    pageView.OnPageChanged = pageChanged;
  }
 
  void pageChanged (int index) {
    pageNumber.text = string.Format ("當前頁碼:{0}",index.ToString ());
  }
 
  public void onClick () {
    try {
      int idnex = int.Parse (inputField.text);
      pageView.pageTo (idnex);
    } catch(Exception ex) {
      Debug.LogWarning ("請輸入數字"+ex.ToString()); 
    }
  }
 
  void Destroy () {
    pageView.OnPageChanged = null;
  }
}

附上專案:地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。