如何用Unity UGUI实现滑动翻页效果来展示长尾词?

2026-03-31 12:131阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计830个文字,预计阅读时间需要4分钟。

如何用Unity UGUI实现滑动翻页效果来展示长尾词?

本文以实例分享如何使用Unity+UGUI实现滑动翻页效果的代码,供大家参考学习。

以下是一个简单的滑动翻页效果实现:

csharpusing UnityEngine;using UnityEngine.UI;

public class ScrollPage : MonoBehaviour{ public ScrollRect scrollRect; public float pageThreshold=0.1f;

private float touchStartPos=0;

void Update() { if (Input.touchCount > 0) { Touch touch=Input.GetTouch(0);

switch (touch.phase) { case TouchPhase.Began: touchStartPos=touch.position.y; break; case TouchPhase.Moved: if (Mathf.Abs(touch.position.y - touchStartPos) > pageThreshold) { scrollRect.content.position -=new Vector3(0, touch.position.y - touchStartPos, 0); } break; } } }}

这段代码实现了基本的滑动翻页效果。首先,创建一个`ScrollRect`组件并拖拽到对应的GameObject上。然后在脚本中设置`scrollRect`变量为这个组件。`pageThreshold`变量用于控制滑动翻页的灵敏度。

在`Update`函数中,当检测到触摸事件时,获取触摸开始位置和移动过程中的位置。如果触摸位置的变化超过阈值`pageThreshold`,则根据触摸位置的偏移量移动`scrollRect.content`的位置,从而实现翻页效果。

注意,此代码仅为示例,具体实现可能需要根据实际情况进行调整。

本文实例为大家分享了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; } }

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

如何用Unity UGUI实现滑动翻页效果来展示长尾词?

剩下的就没有什么了。

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

本文共计830个文字,预计阅读时间需要4分钟。

如何用Unity UGUI实现滑动翻页效果来展示长尾词?

本文以实例分享如何使用Unity+UGUI实现滑动翻页效果的代码,供大家参考学习。

以下是一个简单的滑动翻页效果实现:

csharpusing UnityEngine;using UnityEngine.UI;

public class ScrollPage : MonoBehaviour{ public ScrollRect scrollRect; public float pageThreshold=0.1f;

private float touchStartPos=0;

void Update() { if (Input.touchCount > 0) { Touch touch=Input.GetTouch(0);

switch (touch.phase) { case TouchPhase.Began: touchStartPos=touch.position.y; break; case TouchPhase.Moved: if (Mathf.Abs(touch.position.y - touchStartPos) > pageThreshold) { scrollRect.content.position -=new Vector3(0, touch.position.y - touchStartPos, 0); } break; } } }}

这段代码实现了基本的滑动翻页效果。首先,创建一个`ScrollRect`组件并拖拽到对应的GameObject上。然后在脚本中设置`scrollRect`变量为这个组件。`pageThreshold`变量用于控制滑动翻页的灵敏度。

在`Update`函数中,当检测到触摸事件时,获取触摸开始位置和移动过程中的位置。如果触摸位置的变化超过阈值`pageThreshold`,则根据触摸位置的偏移量移动`scrollRect.content`的位置,从而实现翻页效果。

注意,此代码仅为示例,具体实现可能需要根据实际情况进行调整。

本文实例为大家分享了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; } }

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

如何用Unity UGUI实现滑动翻页效果来展示长尾词?

剩下的就没有什么了。

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