当前位置: 首页 > news >正文

unity入门:按钮控制横向滚动视窗显示最左最右

unity入门:按钮控制横向滚动视窗显示最左最右

  • 实现逻辑
      • 步骤概述
  • 1. UI结构设置
  • 2. 编写控制脚本
  • 3. 关联按钮事件
  • 平滑滚动

实现逻辑

在Unity中实现点击按钮让左右滚动视图到达最左或最右的效果,核心是通过代码控制Scroll Rect组件的horizontalNormalizedPosition属性。

步骤概述

设置UI结构: 确保你的场景中有一个Scroll View(包含Scroll Rect组件)和两个按钮(例如"左"和"右")。

编写控制脚本:创建一个脚本,该脚本引用ScrollRect组件,并包含让滚动视图滚动到最左或最右的方法。

关联按钮事件:将按钮的OnClick()事件与脚本中的相应方法关联起来。

1. UI结构设置

首先,确保你的Unity场景中有一个标准的Scroll View,并且其Scroll Rect组件的Horizontal选项已启用(允许水平滚动)。同时创建两个按钮(UI -> Button),分别用于触发滚动到最左和最右的动作。

2. 编写控制脚本

创建一个新的C#脚本,例如ScrollViewHorizontalController,并将其附加到Scroll View游戏对象或任何合适的游戏对象上(如场景管理器),代码如下。

using UnityEngine;
using UnityEngine.UI;public class ScrollViewHorizontalController : MonoBehaviour
{public ScrollRect targetScrollRect; // 引用你的ScrollRect组件// 滚动到最左边 (horizontalNormalizedPosition = 0f)public void ScrollToLeft(){if (targetScrollRect != null){targetScrollRect.horizontalNormalizedPosition = 0f;}else{Debug.LogWarning("Target ScrollRect is not assigned!");}}// 滚动到最右边 (horizontalNormalizedPosition = 1f)public void ScrollToRight(){if (targetScrollRect != null){targetScrollRect.horizontalNormalizedPosition = 1f;}else{Debug.LogWarning("Target ScrollRect is not assigned!");}}
}

3. 关联按钮事件

1.在Unity编辑器中,选择你的左或右按钮。
2.在Inspector窗口中,找到Button组件的On Click () 事件。
3.点击"+"添加一个新的触发事件。
4.将包含ScrollViewHorizontalController脚本的游戏对象拖拽到事件栏的None (Object) 区域。
5.在下拉菜单中,选择ScrollViewHorizontalController -> ScrollToLeft(或者ScrollToRight,对于“To Right”按钮)。

或在代码中创建public Button Left然后 Left.onClick.AddListener(abc);绑定按钮。

完成上面操作点击绑定的按钮滚动视图就能自动滑动到最左或最右了


平滑滚动

如果你不希望立即跳转,而是希望有一个平滑的动画过渡到最左或最右,可以使用协程(Coroutine)和Mathf.Lerp进行插值,代码如下。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;public class ScrollViewSmoothController : MonoBehaviour
{public ScrollRect targetScrollRect;public float smoothTime = 0.3f; // 平滑滚动所需时间(秒)private Coroutine smoothCoroutine;public void SmoothScrollToLeft(){if (targetScrollRect != null){if (smoothCoroutine != null){StopCoroutine(smoothCoroutine);}smoothCoroutine = StartCoroutine(SmoothScroll(0f));}}public void SmoothScrollToRight(){if (targetScrollRect != null){if (smoothCoroutine != null){StopCoroutine(smoothCoroutine);}smoothCoroutine = StartCoroutine(SmoothScroll(1f));}}private IEnumerator SmoothScroll(float targetPosition){float startPosition = targetScrollRect.horizontalNormalizedPosition;float elapsedTime = 0f;while (elapsedTime < smoothTime){elapsedTime += Time.deltaTime;float newPosition = Mathf.Lerp(startPosition, targetPosition, elapsedTime / smoothTime);targetScrollRect.horizontalNormalizedPosition = newPosition;yield return null;}// 确保最终位置准确targetScrollRect.horizontalNormalizedPosition = targetPosition;smoothCoroutine = null;}
}

文章转载自:

http://Y8P8YfQJ.gychx.cn
http://iUKdm8LB.gychx.cn
http://7hrBvop2.gychx.cn
http://JsvzOBRZ.gychx.cn
http://NVYCqqT4.gychx.cn
http://zlxJ0GB5.gychx.cn
http://2GnS3lVc.gychx.cn
http://0qGQgI3H.gychx.cn
http://RYkCpFGN.gychx.cn
http://BsrSnuWt.gychx.cn
http://4NoVNOyu.gychx.cn
http://RSWuFoTh.gychx.cn
http://WHFuazPw.gychx.cn
http://WrIhkOhQ.gychx.cn
http://fqyRyazc.gychx.cn
http://fgxah0sH.gychx.cn
http://UzPEWEe5.gychx.cn
http://Td011MVp.gychx.cn
http://LlyGTsdb.gychx.cn
http://dfABDgXH.gychx.cn
http://sUyxkPAk.gychx.cn
http://7hgEf6Gp.gychx.cn
http://fKIB36jO.gychx.cn
http://Q2sz7R1w.gychx.cn
http://URedWRUA.gychx.cn
http://ModtEPlO.gychx.cn
http://axTZ2Am8.gychx.cn
http://Ktr8xPIF.gychx.cn
http://qVqHJj7M.gychx.cn
http://fDko7naK.gychx.cn
http://www.dtcms.com/a/373162.html

相关文章:

  • 大模型为什么会有幻觉?-Why Language Models Hallucinate
  • 数据结构造神计划第三天---数据类型
  • MYSQL集群高可用架构之MHA高可用架构
  • 小麦矩阵系统:让短视频分发实现抖音快手小红书全覆盖
  • 智能高低压地埋线走向探测器如何在多条电缆中查找特定电缆?
  • 【Docker】常见操作
  • Python/JS/Go/Java同步学习(第七篇)四语言“字符串类型验证“对照表: 运维“雏田“白眼审核凭证上传崩溃(附源码/截图/参数表/避坑指南)
  • 深入解析网通核心器件:光模块、巴伦(Balun)与LTCC及其关键参数
  • 将 maven 集成到 idea 后出现 向项目创建模块时出错:null 的问题
  • 4.1Vue基本使用
  • 温补晶振(TCXO)
  • 应用层————HTTPS协议
  • 内存越界引发线程函数调用堆栈回溯异常以及INT 3软中断实战分析案例分享
  • stm32——单总线,DHT11
  • WAF(Web应用防火墙)重要域名接入方案 - 操作笔记
  • [网络入侵AI检测] 纯卷积神经网络(CNN)模型 | CNN处理数据
  • Embedding 层(tf.keras.layers.Embedding)文档翻译
  • 人工智能学习:Transformer结构(文本嵌入及其位置编码器)
  • 计算机视觉——光流法
  • VMware 如何创建链接克隆虚拟机
  • Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
  • 【回眸】Tessy 基础操作指南
  • 更智能的零售终端设备管理:合规、安全与高效
  • TCP/IP、HTTP 和 HTTPS简介
  • UNBIASED WATERMARK:大语言模型的无偏差水印
  • Android Studio处理异常报错:Cause connect timed out
  • 基于哈塞特独立性表态的AI量化研究:美联储政策独立性的多维验证
  • 人工智能-python-深度学习-经典神经网络AlexNet
  • SpringBoot集成电科金仓数据库(上):环境搭建与基础配置
  • AR 巡检与普通巡检有哪些区别,有哪些优势|阿法龙XR云平台