欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

unity scrollRect實(shí)現(xiàn)按頁碼翻頁效果

 更新時(shí)間:2020年04月18日 08:54:38   作者:liu_sanad  
這篇文章主要為大家詳細(xì)介紹了unity scrollRect實(shí)現(xiàn)按頁碼翻頁效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了unity實(shí)現(xiàn)按頁碼翻頁效果的具體代碼,供大家參考,具體內(nèi)容如下

用來做背包 商店的按頁翻頁功能,先上效果圖

其中,dragNum 表示的是 如果為3,便是滑動(dòng)距離超過當(dāng)前頁面寬度的百分之三十位成功

connect表示 scrollRect下connet的大小

然后是函數(shù)的初始化方法 這里提供了兩個(gè)方法  一個(gè)是直接提供頁數(shù) 參數(shù)分別代表 總頁數(shù),要顯示的頁數(shù) 以及切換到要顯示的頁數(shù)是否播放滑動(dòng)動(dòng)畫

以及當(dāng)時(shí)用來做背包、商店的方法2

和上面不用的是傳入的是物品的數(shù)量以及每頁顯示格子數(shù)量的vector2 是否需要改變connect的大小 (需要提前把connect的大小設(shè)置為一頁的大?。┮约按蜷_時(shí)顯示的頁數(shù)

思路大致如下 繼承 IBeginDragHandler, IEndDragHandler兩個(gè)接口

在begin時(shí)將需要的兩個(gè)參數(shù) scrollNeedMove 以及scrollTargetValue 置位默認(rèn)值

在end時(shí)計(jì)算

具體代碼如下

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ScrollPageTool : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{

 public Button pageLastButton;
 public Button pageNextButton;
 public Text pageNumText;
 public GridLayoutGroup itemConnect;
 [SerializeField]
 private bool buttonPageEnable;
 private int m_nowPage;//從0開始
 private int m_pageCount;
 /// <summary>
 /// 滑動(dòng)距離超過一頁的 (m_dragNum*10)% 則滑動(dòng)成功
 /// </summary>
 public int m_dragNum;
 private float m_pageAreaSize;
 private const float SCROLL_SMOOTH_TIME = 0.2F;
 private const float SCROLL_MOVE_SPEED = 1F;
 private float scrollMoveSpeed = 0f;
 private bool scrollNeedMove = false;
 private float scrollTargetValue;
 public ScrollRect scrollRect;
 
 private bool isRegistEvent = false;
 
 public bool SetButtonStatus
 {
 set
 {
 buttonPageEnable = value;
 pageLastButton.interactable = buttonPageEnable && pageLastButton.interactable;
 pageNextButton.interactable = buttonPageEnable && pageNextButton.interactable;
 }
 }
 public void InitManager(int _allItemNum, Vector2 pageItemSize, bool isNeedChangeSize = true, int pageNum = 0,bool isShowAnim=false)
 {
 RegistEvent();
 int _pageItemNum = (int)(pageItemSize.x * pageItemSize.y);
 m_pageCount = (_allItemNum / _pageItemNum) + ((_allItemNum % _pageItemNum == 0) ? 0 : 1);
 m_pageAreaSize = 1f / (m_pageCount - 1);
 ChangePage(pageNum);
 if (isNeedChangeSize)
 itemConnect.GetComponent<RectTransform>().sizeDelta = new Vector2((itemConnect.cellSize.x * pageItemSize.x + itemConnect.spacing.x * pageItemSize.x) * m_pageCount,
 itemConnect.padding.top + itemConnect.padding.bottom + itemConnect.cellSize.y * pageItemSize.y + (pageItemSize.y - 1) * itemConnect.spacing.y);
 }
 
 public void InitManager(int pageNum, int targetPage = 0, bool isShowAnim = false)
 {
 RegistEvent();
 m_pageCount = pageNum;
 ChangePage(targetPage, isShowAnim);
 }
 
 private void RegistEvent()
 {
 if (isRegistEvent)
 return;
 isRegistEvent = true;
 if (pageLastButton != null)
 pageLastButton.onClick.AddListener(delegate { Paging(-1); });
 if (pageNextButton != null)
 pageNextButton.onClick.AddListener(delegate { Paging(1); });
 }
 
 private void Paging(int num)
 {
 //maxNum-1,從0開始
 num = (num < 0) ? -1 : 1;
 int temp = Mathf.Clamp(m_nowPage + num, 0, m_pageCount - 1);
 if (m_nowPage == temp)
 return;
 ChangePage(temp);
 }
 void Update()
 {
 ScrollControl();
 }
 
 public int GetPageNum { get { return m_nowPage; } }
 //按頁翻動(dòng)
 private void ScrollControl()
 {
 if (!scrollNeedMove)
 return;
 if (Mathf.Abs(scrollRect.horizontalNormalizedPosition - scrollTargetValue) < 0.01f)
 {
 scrollRect.horizontalNormalizedPosition = scrollTargetValue;
 scrollNeedMove = false;
 return;
 }
 scrollRect.horizontalNormalizedPosition = Mathf.SmoothDamp(scrollRect.horizontalNormalizedPosition, scrollTargetValue, ref scrollMoveSpeed, SCROLL_SMOOTH_TIME);
 }
 
 public void OnBeginDrag(PointerEventData eventData)
 {
 scrollNeedMove = false;
 scrollTargetValue = 0;
 }
 public void OnEndDrag(PointerEventData eventData)
 {
 int tempPage = m_nowPage;
 
 int num=(((scrollRect.horizontalNormalizedPosition - (m_nowPage * m_pageAreaSize))>=0)?1:-1);
 
 if(Mathf.Abs(scrollRect.horizontalNormalizedPosition - (m_nowPage * m_pageAreaSize))>= (m_pageAreaSize / 10f) * m_dragNum)
 {
 tempPage+=num;
 
 }
 ChangePage(tempPage);
 }
 
 public void ChangePage(int pageNum, bool isShowAnim = true)
 {
 if (pageNum >= m_pageCount)
 pageNum = m_pageCount - 1;
 if (pageNum < 0)
 pageNum = 0;
 
 m_nowPage = pageNum;
 ChangePageText(pageNum);
 if (isShowAnim)
 {
 scrollTargetValue = m_nowPage * m_pageAreaSize;
 scrollNeedMove = true;
 scrollMoveSpeed = 0;
 }
 else
 {
 scrollRect.horizontalNormalizedPosition = m_nowPage * m_pageAreaSize;
 }
 ChangePageText(m_nowPage);
 }
 
 public void ChangePageText(int num)
 {
 int maxPageTo0Start = m_pageCount - 1;
 m_nowPage = Mathf.Clamp(num, 0, maxPageTo0Start);
 
 if (pageNumText != null)
 pageNumText.text = (m_nowPage + 1).ToString() + "/" + m_pageCount.ToString();
 
 //only one page
 if (maxPageTo0Start == 0)
 {
 scrollRect.enabled = false;
 pageLastButton.interactable = false;
 pageNextButton.interactable = false;
 return;
 }
 else
 {
 pageLastButton.interactable = true;
 pageNextButton.interactable = true;
 scrollRect.enabled = true;
 }
 SetButtonStatus = buttonPageEnable;
 if (!buttonPageEnable)
 return;
 
 if (m_nowPage == 0 && pageLastButton.interactable)
 pageLastButton.interactable = false;
 if (m_nowPage >= maxPageTo0Start && pageNextButton.interactable)
 pageNextButton.interactable = false;
 if (m_nowPage > 0 && (!pageLastButton.interactable))
 pageLastButton.interactable = true;
 if (m_nowPage < maxPageTo0Start && (!pageNextButton.interactable))
 pageNextButton.interactable = true;
 
 
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論