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

Unity UGUI實現(xiàn)滑動翻頁效果

 更新時間:2020年04月17日 17:08:19   作者:同燈花城  
這篇文章主要為大家詳細介紹了Unity UGUI實現(xiàn)滑動翻頁效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Unity UGUI實現(xiàn)滑動翻頁效果的具體代碼,供大家參考,具體內容如下

這個問題真的是老生常談的事情了,不過在這里還是要說一下,以便以后之需

首先看一下效果圖

最后在Content下面是一些Image

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using System;
 
public class PageView : MonoBehaviour, IBeginDragHandler, IEndDragHandler {
  ScrollRect rect;      //滑動組件 
 //public ScrollRect rect2;      //滑動組件2 
 
 private float targethorizontal = 0;    //滑動的起始坐標 
 private bool isDrag = false;     //是否拖拽結束 
 private List<float> posList = new List<float> ();//求出每頁的臨界角,頁索引從0開始 
 private int currentPageIndex = -1;
 public Action<int> OnPageChanged;
 
 private bool stopMove = true;
 public float smooting = 4;  //滑動速度 
 public float sensitivity = 0;
 private float startTime;
 
 private float startDragHorizontal; 
 
 
 void Awake () {
  // rect = rect2;
  rect = transform.GetComponent<ScrollRect> ();
  // rect2 = transform.GetComponent<ScrollRect>();
  float horizontalLength = rect.content.rect.width - GetComponent<RectTransform> ().rect.width;
  //float horizontalLength2 = rect2.content.rect.width - GetComponent<RectTransform>().rect.width;
  posList.Add (0);
  for(int i = 1; i < rect.content.transform.childCount - 1; i++) {
   posList.Add (GetComponent<RectTransform> ().rect.width * i / horizontalLength);
  }
  posList.Add (1);
 }
 
 void Update () {
  if(!isDrag && !stopMove) {
   startTime += Time.deltaTime;
   float t = startTime * smooting;
   rect.horizontalNormalizedPosition = Mathf.Lerp (rect.horizontalNormalizedPosition , targethorizontal , t);
   // rect2.horizontalNormalizedPosition = Mathf.Lerp(rect2.horizontalNormalizedPosition, targethorizontal, t);
   if (t >= 1)
    stopMove = true;
  }
 }
 
 public void pageTo (int index) {
  if(index >= 0 && index < posList.Count) {
   rect.horizontalNormalizedPosition = posList[index];
   SetPageIndex(index);
  } else {
   Debug.LogWarning ("頁碼不存在");
  }
 }
 private void SetPageIndex (int index) {
  if(currentPageIndex != index) {
   currentPageIndex = index;
   if(OnPageChanged != null)
    OnPageChanged (index);
  }
 }
 
 public void OnBeginDrag (PointerEventData eventData) {
  isDrag = true;
  startDragHorizontal = rect.horizontalNormalizedPosition;
  // startDragHorizontal = rect2.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);
  for(int i = 1; i < posList.Count; i++) {
   float temp = Mathf.Abs (posList[i] - posX);
   if(temp < offset) {
    index = i;
    offset = temp;
   }
  }
  SetPageIndex (index);
 
  targethorizontal = posList[index]; //設置當前坐標,更新函數(shù)進行插值 
  isDrag = false;
  startTime = 0;
  stopMove = false;
 } 
}

最后看一下,怎么設置的:

剩下的就沒有什么了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#使用Shader實現(xiàn)夜幕降臨倒計時的效果

    C#使用Shader實現(xiàn)夜幕降臨倒計時的效果

    這篇文章主要介紹了C#使用Shader實現(xiàn)夜幕降臨倒計時的效果,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • C#基于TimeSpan實現(xiàn)倒計時效果的方法

    C#基于TimeSpan實現(xiàn)倒計時效果的方法

    這篇文章主要介紹了C#基于TimeSpan實現(xiàn)倒計時效果的方法,比較實用的功能,需要的朋友可以參考下
    2014-08-08
  • C#關于Task.Yeild()函數(shù)的討論

    C#關于Task.Yeild()函數(shù)的討論

    這篇文章主要介紹了C#中關于Task.Yeild()函數(shù)的相關資料,文中講解非常細致,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • C#實現(xiàn)的AES加密解密完整實例

    C#實現(xiàn)的AES加密解密完整實例

    這篇文章主要介紹了C#實現(xiàn)的AES加密解密,結合完整實例形式分析了C#實現(xiàn)的AES算法進行加密與解密的相關技巧,需要的朋友可以參考下
    2016-07-07
  • 區(qū)分WCF與WebService的異同、優(yōu)勢

    區(qū)分WCF與WebService的異同、優(yōu)勢

    這篇文章主要幫助大家區(qū)分WCF與WebService的異同、優(yōu)勢,分為三大方面進行研究學習,感興趣的小伙伴們可以參考一下
    2016-03-03
  • C# 拼圖魔方小游戲

    C# 拼圖魔方小游戲

    這篇文章主要介紹了C# 拼圖魔方小游戲,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • C#將DataGridView中的數(shù)據(jù)保存到CSV和Excel中

    C#將DataGridView中的數(shù)據(jù)保存到CSV和Excel中

    這篇文章介紹了C#將DataGridView中的數(shù)據(jù)保存到CSV和Excel中的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • Unity實現(xiàn)桌面反彈的示例代碼

    Unity實現(xiàn)桌面反彈的示例代碼

    反彈球是小時候都玩過的網(wǎng)頁小游戲,但是很多人都不知道怎樣實現(xiàn),本文就來介紹一下Unity實現(xiàn)桌面反彈的示例代碼,感興趣的可以了解一下
    2021-05-05
  • C#?Winform實現(xiàn)復制文件顯示進度

    C#?Winform實現(xiàn)復制文件顯示進度

    這篇文章主要介紹了C#?Winform實現(xiàn)復制文件顯示進度,用進度條來顯示復制情況,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • C#編程自學之數(shù)據(jù)類型和變量三

    C#編程自學之數(shù)據(jù)類型和變量三

    C#語言類型系統(tǒng)提出的一個核心概念裝箱(boxing)拆箱(unboxing)。裝箱和取消裝箱的概念是C#的類型系統(tǒng)的核心。它在“值類型”和“引用類型”之間的架起了一座橋梁,使得任何“值類型”的值都可以轉換為object類型的值,反過來轉換也可以。
    2015-10-10

最新評論