Unity UGUI實(shí)現(xiàn)滑動(dòng)翻頁(yè)直接跳轉(zhuǎn)頁(yè)數(shù)
本文實(shí)例為大家分享了Unity UGUI實(shí)現(xiàn)滑動(dòng)翻頁(yè),直接跳轉(zhuǎn)頁(yè)數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
首先看一下最終效果


其實(shí)這個(gè)功能基本上是老生常談了,所以代碼還是很簡(jiǎn)單
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; //滑動(dòng)組件
private float targethorizontal = 0; //滑動(dòng)的起始坐標(biāo)
private bool isDrag = false; //是否拖拽結(jié)束
private List<float> posList = new List<float>(); //求出每頁(yè)的臨界角,頁(yè)索引從0開(kāi)始
private int currentPageIndex = -1;
public Action<int> OnPageChanged;
public RectTransform content;
private bool stopMove = true;
public float smooting = 4; //滑動(dòng)速度
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);
//未顯示的長(zhǎng)度
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;
//開(kāi)始拖動(dòng)
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]; //設(shè)置當(dāng)前坐標(biāo),更新函數(shù)進(jìn)行插值
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 ("當(dāng)前頁(yè)碼:0");
pageView.OnPageChanged = pageChanged;
}
void pageChanged (int index) {
pageNumber.text = string.Format ("當(dāng)前頁(yè)碼:{0}" , index.ToString ());
}
public void onClick () {
try {
int idnex = int.Parse (inputField.text);
pageView.pageTo (idnex);
} catch(Exception ex) {
Debug.LogWarning ("請(qǐng)輸入數(shù)字"+ex.ToString());
}
}
void Destroy () {
pageView.OnPageChanged = null;
}
}
附上項(xiàng)目:地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#語(yǔ)言使用Unity粒子系統(tǒng)制作手雷爆炸
這篇文章主要為大家介紹了Unity的粒子系統(tǒng)由粒子發(fā)射器、粒子動(dòng)畫器、粒子渲染器組成,通過(guò)使用一或兩個(gè)紋理多次繪制,創(chuàng)造一個(gè)混沌的效果,通過(guò)復(fù)習(xí)粒子系統(tǒng)做一個(gè)手雷和實(shí)彈投擲現(xiàn)場(chǎng)2022-04-04
VS中模仿WPF模板創(chuàng)建最簡(jiǎn)單的WPF程序
這篇文章主要為大家詳細(xì)介紹了VS中模仿WPF模板創(chuàng)建最簡(jiǎn)單的WPF程序的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
手把手教你如何基于C#制作一個(gè)網(wǎng)址檢測(cè)工具
這篇文章主要給大家介紹了關(guān)于如何基于C#制作一個(gè)網(wǎng)址檢測(cè)工具的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02
Unity Shader實(shí)現(xiàn)序列幀動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)序列幀動(dòng)畫效果 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02

