Unity使用ScrollRect制作翻頁
本文實(shí)例為大家分享了使用ScrollRect制作翻頁的具體代碼,供大家參考,具體內(nèi)容如下
1.標(biāo)準(zhǔn)的層級(jí)結(jié)構(gòu) ScrollRect->ViewPort->Content,Viewport負(fù)責(zé)顯示區(qū)域的大小一般和Mask一起配合使用,Content使用Layout來布局,如果想使用代碼來自動(dòng)定位顯示位置需要在Content加上Content size filter.
2.ScrollRectHelper
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System;
public class ScrollRectHelper : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
// 滑動(dòng)速度
public float smooting = 5;
// 每頁顯示的項(xiàng)目
[SerializeField]
private int countPerPage = 10;
ScrollRect srect;
// 總頁數(shù)
float totalPages;
// 是否拖拽結(jié)束
bool isDrag = false;
// 總頁數(shù)索引比列 0-1
List<float> listPageValue = new List<float> { 0 };
// 滑動(dòng)的目標(biāo)位置
public float targetPos = 0;
// 當(dāng)前位置索引
float nowindex = 0;
void Awake()
{
srect = GetComponent<ScrollRect>();
}
public string PageText()
{
return (nowindex + 1) + "/" + (totalPages + 1);
}
// 計(jì)算每頁比例
public void CalcListPageValue<T>() where T : MonoBehaviour
{
T[] items = srect.content.GetComponentsInChildren<T>();
srect.content.rect.Set(srect.content.rect.width / 2, srect.content.rect.y, srect.content.rect.width, srect.content.rect.height);
totalPages = (int)(Math.Ceiling((float)items.Length / countPerPage) - 1);
if (items.Length != 0)
{
for (float i = 1; i <= totalPages; i++)
{
//Debug.Log(i / totalPages);
listPageValue.Add((i / totalPages));
}
}
}
void Update()
{
if (!isDrag)
{
srect.horizontalNormalizedPosition = Mathf.Lerp(srect.horizontalNormalizedPosition, targetPos,
Time.deltaTime * smooting);
}
// Debug
if (Input.GetKeyDown(KeyCode.LeftArrow)) PressLeft();
if (Input.GetKeyDown(KeyCode.RightArrow)) PressRight();
}
/// <summary>
/// 拖動(dòng)開始
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData)
{
isDrag = true;
}
/// <summary>
/// 拖拽結(jié)束
/// </summary>
/// <param name="eventData"></param>
public void OnEndDrag(PointerEventData eventData)
{
isDrag = false;
var tempPos = srect.horizontalNormalizedPosition; //獲取拖動(dòng)的值
var index = 0;
float offset = Mathf.Abs(listPageValue[index] - tempPos); //拖動(dòng)的絕對(duì)值
for (int i = 1; i < listPageValue.Count; i++)
{
float temp = Mathf.Abs(tempPos - listPageValue[i]);
if (temp < offset)
{
index = i;
offset = temp;
}
}
targetPos = listPageValue[index];
nowindex = index;
}
public void PressLeft()
{
nowindex = Mathf.Clamp(nowindex - 1, 0, totalPages);
targetPos = listPageValue[Convert.ToInt32(nowindex)];
}
public void PressRight()
{
nowindex = Mathf.Clamp(nowindex + 1, 0, totalPages);
targetPos = listPageValue[Convert.ToInt32(nowindex)];
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WPF利用ValueConverter實(shí)現(xiàn)值轉(zhuǎn)換器
值轉(zhuǎn)換器在WPF開發(fā)中是非常常見的,值轉(zhuǎn)換器可以幫助我們很輕松地實(shí)現(xiàn),界面數(shù)據(jù)展示的問題。本文將通過WPF?ValueConverter實(shí)現(xiàn)簡(jiǎn)單的值轉(zhuǎn)換器,希望對(duì)大家有所幫助2023-03-03
c# volatile 關(guān)鍵字的拾遺補(bǔ)漏
這篇文章主要介紹了c# volatile 關(guān)鍵字的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#的相關(guān)知識(shí),感興趣的朋友可以了解下2020-10-10
C#中使用CAS實(shí)現(xiàn)無鎖算法的示例詳解
CAS(Compare-and-Swap)是一種多線程并發(fā)編程中常用的原子操作,用于實(shí)現(xiàn)多線程間的同步和互斥訪問。本文將利用CAS實(shí)現(xiàn)無鎖算法,需要的可以參考一下2023-04-04
C#中調(diào)用SAPI實(shí)現(xiàn)語音合成的2種方法
這篇文章主要介紹了C#中調(diào)用SAPI實(shí)現(xiàn)語音合成的2種方法,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06
C#中事件的動(dòng)態(tài)調(diào)用實(shí)現(xiàn)方法
這篇文章主要介紹了C#中事件的動(dòng)態(tài)調(diào)用實(shí)現(xiàn)方法,對(duì)比傳統(tǒng)思路優(yōu)劣給出了一個(gè)新的解決方案,需要的朋友可以參考下2014-09-09

