最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

UGUI ScrollRect實(shí)現(xiàn)帶按鈕翻頁(yè)支持拖拽

 更新時(shí)間:2020年05月22日 09:06:50   作者:代碼妖  
這篇文章主要為大家詳細(xì)介紹了UGUI ScrollRect實(shí)現(xiàn)帶按鈕翻頁(yè)支持拖拽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了UGUI ScrollRect帶按鈕翻頁(yè)支持拖拽的具體代碼,供大家參考,具體內(nèi)容如下

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System;
/// <summary>
/// 略知CSharp 
/// </summary>
public class ScrollRectHelper : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
 
 public float smooting = 5;    //滑動(dòng)速度
 public List<GameObject> listItem;   //scrollview item 
 public int pageCount = 3;    //每頁(yè)顯示的項(xiàng)目
 
 ScrollRect srect;
 float pageIndex;     //總頁(yè)數(shù)
 bool isDrag = false;    //是否拖拽結(jié)束
 List<float> listPageValue = new List<float> { 0 }; //總頁(yè)數(shù)索引比列 0-1
 float targetPos = 0;    //滑動(dòng)的目標(biāo)位置
 float nowindex = 0;     //當(dāng)前位置索引
 
 void Awake()
 {
 srect = GetComponent<ScrollRect>();
 ListPageValueInit();
 }
 
 //每頁(yè)比例
 void ListPageValueInit()
 {
 pageIndex = (listItem.Count / pageCount)-1;
 if (listItem != null && listItem.Count != 0)
 {
  for (float i = 1; i <= pageIndex; i++)
  {
  listPageValue.Add((i / pageIndex));
  }
 }
 }
 
 void Update()
 {
 if (!isDrag)
  srect.horizontalNormalizedPosition = Mathf.Lerp(srect.horizontalNormalizedPosition, targetPos, Time.deltaTime * smooting);
 }
 /// <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 BtnLeftGo()
 {
 nowindex = Mathf.Clamp(nowindex - 1, 0, pageIndex);
 targetPos = listPageValue[Convert.ToInt32(nowindex)];
 }
 
 public void BtnRightGo()
 {
 nowindex = Mathf.Clamp(nowindex + 1, 0, pageIndex);
 targetPos = listPageValue[Convert.ToInt32(nowindex)];
 
 }
}

DEMO 下載地址

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

相關(guān)文章

最新評(píng)論

哈尔滨市| 奉新县| 隆化县| 上林县| 恩平市| 大港区| 和硕县| 宁蒗| 江华| 盐池县| 会宁县| 常山县| 即墨市| 内丘县| 泸西县| 石嘴山市| 永川市| 广丰县| 昌江| 襄樊市| 炎陵县| 克山县| 平舆县| 慈利县| 观塘区| 万安县| 长宁县| 大港区| 喀什市| 平利县| 垣曲县| 承德县| 云林县| 元朗区| 西吉县| 浏阳市| 米易县| 湄潭县| 瑞安市| 拉孜县| 宣汉县|