Unity UGUI实现滑动翻页效果

 更新时间:2020年04月17日 17:08:19   作者:同灯花城  
这篇文章主要为大家详细介绍了Unity UGUI实现滑动翻页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

这个问题真的是老生常谈的事情了,不过在这里还是要说一下,以便以后之需

首先看一下效果图

最后在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]; //设置当前坐标,更新函数进行插值 
  isDrag = false;
  startTime = 0;
  stopMove = false;
 } 
}

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

剩下的就没有什么了。

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

相关文章

  • C#命令模式用法实例

    C#命令模式用法实例

    这篇文章主要介绍了C#命令模式用法,以实例形式较为详细的分析了C#命令模式的功能、定义及使用技巧,需要的朋友可以参考下
    2015-07-07
  • C#实现绘制随机噪点和直线

    C#实现绘制随机噪点和直线

    这篇文章主要为大家详细介绍了C#如何实现绘制随机噪点和直线,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2023-01-01
  • C#中DataTable删除行的方法分析

    C#中DataTable删除行的方法分析

    这篇文章主要介绍了C#中DataTable删除行的方法,包括了常见的几种删除方法的分析,需要的朋友可以参考下
    2014-09-09
  • C# 如何使用OpcUaHelper读写OPC服务器

    C# 如何使用OpcUaHelper读写OPC服务器

    这篇文章给大家介绍C# 如何使用OpcUaHelper读写OPC服务器,本文通过图文实例代码相结合给大家介绍的非常详细,需要的朋友参考下吧
    2023-12-12
  • WPF实现平面三角形3D运动效果

    WPF实现平面三角形3D运动效果

    这篇文章主要为大家详细介绍了WPF实现平面三角形3D运动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • C#中跨线程访问控件的实现方法

    C#中跨线程访问控件的实现方法

    C#中不允许跨线程直接访问界面控件,即一个线程中如主线程创建的控件不允许被其他线程例如子线程直接访问,在一个线程中设置其他线程所有的控件属性通常有两种方法,本文将详细的给大家介绍一下,需要的朋友可以参考下
    2024-12-12
  • 对int array进行排序的实例讲解

    对int array进行排序的实例讲解

    下面小编就为大家分享一篇对int array进行排序的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • 在C#程序中注入恶意DLL的方法详解

    在C#程序中注入恶意DLL的方法详解

    为什么 Windbg 附加到 C# 程序后,程序就处于中断状态了?它到底是如何实现的?其实简而言之就是线程的远程注入,这一篇就展开说一下
    2022-09-09
  • C#实现的UDP收发请求工具类实例

    C#实现的UDP收发请求工具类实例

    这篇文章主要介绍了C#实现的UDP收发请求工具类,结合具体实例形式分析了C#针对UDP请求的监听、接收、发送等相关操作技巧,需要的朋友可以参考下
    2017-06-06
  • C#中实现契约测试的方法

    C#中实现契约测试的方法

    这篇文章主要介绍了C#中实现契约测试,在本文中,我将揭开契约测试的神秘面纱,并向您展示如何在 C# 项目中实现它,需要的朋友可以参考下
    2023-09-09

最新评论