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-04-04
  • C# ManualResetEvent使用方法详解

    C# ManualResetEvent使用方法详解

    这篇文章主要为大家详细介绍了ManualResetEvent使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • C#.NET中如何批量插入大量数据到数据库中

    C#.NET中如何批量插入大量数据到数据库中

    这篇文章主要给大家介绍C#.net中如何批量插入大量数据到数据库中,本文涉及到C#.net中批量插入数据到数据库中方面的内容,对C#.net批量插入数据到数据库中感兴趣的朋友可以参考下本篇文章
    2015-10-10
  • C++中#include头文件的示例详解

    C++中#include头文件的示例详解

    在C++中,所有的文件操作,都是以流(stream)的方式进行的,fstream也就是文件流file stream。这篇文章主要介绍了C++中#include头文件,需要的朋友可以参考下
    2020-02-02
  • C#简单数字图像处理程序

    C#简单数字图像处理程序

    这篇文章主要为大家详细介绍了C#简单数字图像处理程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • 理解C#中的枚举(简明易懂)

    理解C#中的枚举(简明易懂)

    这篇文章主要介绍了理解C#中的枚举(简明易懂),本文讲解了枚举的优点、枚举说明、枚举的类型、枚举的使用建议等内容,需要的朋友可以参考下
    2015-05-05
  • C#中Winfrom默认输入法的设置方法

    C#中Winfrom默认输入法的设置方法

    这篇文章主要介绍了C#中Winfrom默认输入法的设置方法,以实例形式较为详细的分析了C#中输入法设置的相关技巧,需要的朋友可以参考下
    2015-05-05
  • C#之HttpClient的同步使用方式

    C#之HttpClient的同步使用方式

    这篇文章主要介绍了C#之HttpClient的同步使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • C#将Sql数据保存到Excel文件中的方法

    C#将Sql数据保存到Excel文件中的方法

    这篇文章主要介绍了C#将Sql数据保存到Excel文件中的方法,文中的ExportExcel可起到将sql数据导出为Excel的作用,需要的朋友可以参考下
    2014-08-08
  • .NET实现定时发送邮件代码(两种方式)

    .NET实现定时发送邮件代码(两种方式)

    经常发邮件的朋友都知道,邮箱有个特殊功能,可以设定邮件发送时间,定时发送,这个功能是怎么实现的呢?接下来,小编给大家分享.NET实现定时发送邮件的代码,有需要的朋友可以参考下
    2015-08-08

最新评论