Unity如何使用ScrollRect制作翻頁(yè)

這篇文章主要講解了Unity如何使用ScrollRect制作翻頁(yè),內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、成都做網(wǎng)站、新野網(wǎng)絡(luò)推廣、小程序定制開(kāi)發(fā)、新野網(wǎng)絡(luò)營(yíng)銷(xiāo)、新野企業(yè)策劃、新野品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供新野建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:bm7419.com

1.標(biāo)準(zhǔn)的層級(jí)結(jié)構(gòu) ScrollRect->ViewPort->Content,Viewport負(fù)責(zé)顯示區(qū)域的大小一般和Mask一起配合使用,Content使用Layout來(lái)布局,如果想使用代碼來(lái)自動(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;

  // 每頁(yè)顯示的項(xiàng)目
  [SerializeField]
  private int countPerPage = 10;

  ScrollRect srect;
  // 總頁(yè)數(shù)
  float totalPages;
  // 是否拖拽結(jié)束
  bool isDrag = false;
  // 總頁(yè)數(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ì)算每頁(yè)比例
  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)開(kāi)始
  /// </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)];
  }
}

看完上述內(nèi)容,是不是對(duì)Unity如何使用ScrollRect制作翻頁(yè)有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)標(biāo)題:Unity如何使用ScrollRect制作翻頁(yè)
URL地址:http://bm7419.com/article20/psscco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、微信小程序、網(wǎng)頁(yè)設(shè)計(jì)公司、全網(wǎng)營(yíng)銷(xiāo)推廣手機(jī)網(wǎng)站建設(shè)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)