Unity使用ScrollRect制作翻頁
更新時間:2020年04月17日 10:08:39 作者:非法關(guān)鍵字
這篇文章主要為大家詳細介紹了Unity使用ScrollRect制作翻頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了使用ScrollRect制作翻頁的具體代碼,供大家參考,具體內(nèi)容如下
1.標準的層級結(jié)構(gòu) ScrollRect->ViewPort->Content,Viewport負責顯示區(qū)域的大小一般和Mask一起配合使用,Content使用Layout來布局,如果想使用代碼來自動定位顯示位置需要在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 { // 滑動速度 public float smooting = 5; // 每頁顯示的項目 [SerializeField] private int countPerPage = 10; ScrollRect srect; // 總頁數(shù) float totalPages; // 是否拖拽結(jié)束 bool isDrag = false; // 總頁數(shù)索引比列 0-1 List<float> listPageValue = new List<float> { 0 }; // 滑動的目標位置 public float targetPos = 0; // 當前位置索引 float nowindex = 0; void Awake() { srect = GetComponent<ScrollRect>(); } public string PageText() { return (nowindex + 1) + "/" + (totalPages + 1); } // 計算每頁比例 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> /// 拖動開始 /// </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; //獲取拖動的值 var index = 0; float offset = Mathf.Abs(listPageValue[index] - tempPos); //拖動的絕對值 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)]; } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用三層架構(gòu)開發(fā)Winform的詳細案例
這篇文章介紹了C#使用三層架構(gòu)開發(fā)Winform的詳細案例,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04C#設(shè)計模式之Strategy策略模式解決007大破密碼危機問題示例
這篇文章主要介紹了C#設(shè)計模式之Strategy策略模式解決007大破密碼危機問題,簡單描述了策略模式的定義并結(jié)合加密解密算法實例分析了C#策略模式的具體使用方法,需要的朋友可以參考下2017-09-09