Unity UGUI实现滑动翻页直接跳转页数

 更新时间:2020年04月17日 14:50:18   作者:Unity_阿黄  
这篇文章主要为大家详细介绍了Unity UGUI实现滑动翻页,直接跳转页数,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity UGUI实现滑动翻页,直接跳转页数的具体代码,供大家参考,具体内容如下

首先看一下最终效果

其实这个功能基本上是老生常谈了,所以代码还是很简单

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using System;
 
public class PageView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
 
 private ScrollRect rect;      //滑动组件 
 private float targethorizontal = 0;    //滑动的起始坐标 
 private bool isDrag = false;     //是否拖拽结束 
 private List<float> posList = new List<float>();   //求出每页的临界角,页索引从0开始 
 private int currentPageIndex = -1;
 public Action<int> OnPageChanged;
 public RectTransform content;
 private bool stopMove = true;
 public float smooting = 4;  //滑动速度 
 public float sensitivity = 0;
 private float startTime;
 
 private float startDragHorizontal;
 public Transform toggleList;
 
 void Start()
 {
  rect = transform.GetComponent<ScrollRect>();
  var _rectWidth = GetComponent<RectTransform>();
  var tempWidth = ((float)content.transform.childCount * _rectWidth.rect.width);
  content.sizeDelta = new Vector2(tempWidth, _rectWidth.rect.height);
  //未显示的长度
  float horizontalLength = content.rect.width - _rectWidth.rect.width;
  for (int i = 0; i < rect.content.transform.childCount; i++)
  {
   posList.Add(_rectWidth.rect.width * i / horizontalLength);
  }
 }
 
 void Update()
 {
  if (!isDrag && !stopMove)
  {
   startTime += Time.deltaTime;
   float t = startTime * smooting;
   rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, t);
   if (t >= 1)
    stopMove = true;
  }
  //Debug.Log(rect.horizontalNormalizedPosition);
 }
 
 public void pageTo(int index)
 {
  if (index >= 0 && index < posList.Count)
  {
   rect.horizontalNormalizedPosition = posList[index];
   SetPageIndex(index);
   GetIndex(index);
  }
 }
 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;
 }
 
 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);
  //Debug.Log("offset " + offset);
 
 
  for (int i = 1; i < posList.Count; i++)
  {
   float temp = Mathf.Abs(posList[i] - posX);
   //Debug.Log("temp " + temp);
   //Debug.Log("i" + i);
   if (temp < offset)
   {
    index = i;
    offset = temp;
   }
   //Debug.Log("index " + index);
  }
  //Debug.Log(index);
  SetPageIndex(index);
  GetIndex(index);
  targethorizontal = posList[index]; //设置当前坐标,更新函数进行插值 
  isDrag = false;
  startTime = 0;
  stopMove = false;
 
 }
 
 public void GetIndex(int index)
 {
  var toogle = toggleList.GetChild(index).GetComponent<Toggle>();
  toogle.isOn = true;
 }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
 
public class GameController : MonoBehaviour {
 [SerializeField]
 private Text pageNumber;
 [SerializeField]
 private InputField inputField;
 [SerializeField]
 private PageView pageView;
 // Use this for initialization
 void Start () {
  pageNumber.text = string.Format ("当前页码:0");
  pageView.OnPageChanged = pageChanged;
 }
 
 void pageChanged (int index) {
  pageNumber.text = string.Format ("当前页码:{0}" , index.ToString ());
 }
 
 public void onClick () {
  try {
   int idnex = int.Parse (inputField.text);
   pageView.pageTo (idnex);
  } catch(Exception ex) {
   Debug.LogWarning ("请输入数字"+ex.ToString()); 
  }
 }
 
 void Destroy () {
  pageView.OnPageChanged = null;
 }
}

附上项目:地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Unity3D实现警报灯

    Unity3D实现警报灯

    这篇文章主要为大家详细介绍了Unity3D实现警报灯效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • C#根据权重抽取随机数

    C#根据权重抽取随机数

    最近在开发过程中遇到一个需要做带权随机的处理,本文主要介绍了C#根据权重抽取随机数,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • C#中math类的全部运算方法(总结)

    C#中math类的全部运算方法(总结)

    下面小编就为大家带来一篇C#中math类的全部运算方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • c#数字图像处理的3种方法示例分享

    c#数字图像处理的3种方法示例分享

    这篇文章主要介绍了c#数字图像处理的3种方法示例,需要的朋友可以参考下
    2014-02-02
  • C#使用ML.Net完成人工智能预测

    C#使用ML.Net完成人工智能预测

    这篇文章主要介绍了C#使用ML.Net完成人工智能预测的详细教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • C#验证给定字符串是否为数字的方法

    C#验证给定字符串是否为数字的方法

    这篇文章主要介绍了C#验证给定字符串是否为数字的方法,实例分析了C#进行字符串操作的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03
  • C#表达式和运算符详细解析

    C#表达式和运算符详细解析

    这篇文章主要介绍了C#表达式和运算符详细解析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-07-07
  • C#实现DataGridView控件行列互换的方法

    C#实现DataGridView控件行列互换的方法

    这篇文章主要介绍了C#实现DataGridView控件行列互换的方法,涉及C#中DataGridView控件元素遍历与添加操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • C#生成Word文档代码示例

    C#生成Word文档代码示例

    这篇文章主要介绍了C#生成Word文档代码示例,本文直接给出代码实例,需要的朋友可以参考下
    2015-06-06
  • C#中TransactionScope的使用小结

    C#中TransactionScope的使用小结

    本篇文章主要是对C#中TransactionScope的使用方法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01

最新评论