UGUI ScrollRect實(shí)現(xiàn)帶按鈕翻頁支持拖拽
更新時(shí)間:2020年05月22日 09:06:50 作者:代碼妖
這篇文章主要為大家詳細(xì)介紹了UGUI ScrollRect實(shí)現(xiàn)帶按鈕翻頁支持拖拽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了UGUI ScrollRect帶按鈕翻頁支持拖拽的具體代碼,供大家參考,具體內(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; //每頁顯示的項(xiàng)目
ScrollRect srect;
float pageIndex; //總頁數(shù)
bool isDrag = false; //是否拖拽結(jié)束
List<float> listPageValue = new List<float> { 0 }; //總頁數(shù)索引比列 0-1
float targetPos = 0; //滑動(dòng)的目標(biāo)位置
float nowindex = 0; //當(dāng)前位置索引
void Awake()
{
srect = GetComponent<ScrollRect>();
ListPageValueInit();
}
//每頁比例
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)的絕對值
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 下載地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡單掌握Windows中C#啟動(dòng)外部程序進(jìn)程的方法
這篇文章主要介紹了Windows中C#啟動(dòng)外部程序進(jìn)程的方法,例子中同時(shí)包括了進(jìn)程關(guān)閉的方法,需要的朋友可以參考下2016-03-03
基于C#?實(shí)現(xiàn)?OPC?DA?Server的問題小結(jié)
這篇文章主要介紹了基于C#?實(shí)現(xiàn)?OPC?DA?Server的相關(guān)知識,關(guān)于C#怎么編寫一個(gè)進(jìn)程外的DCOM組件,這里先不做介紹了,這里主要介紹下OPC?DA?Server?的第一個(gè)接口,感興趣的朋友跟隨小編一起看看吧2024-04-04
BarCode條形碼基于C# GDI+ 的實(shí)現(xiàn)方法詳解
本篇文章介紹了,BarCode條形碼基于C# GDI+ 的實(shí)現(xiàn)方法詳解。需要的朋友參考下2013-05-05
C#實(shí)現(xiàn)將PPT轉(zhuǎn)換成HTML的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將PPT轉(zhuǎn)換成HTML的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
winform創(chuàng)建不規(guī)則窗體的方法
這篇文章主要介紹了winform創(chuàng)建不規(guī)則窗體的方法,涉及C#窗體創(chuàng)建的相關(guān)參數(shù)設(shè)置技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-09-09

