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

Unity 鼠标悬浮时文本滚动(Text Mesh Pro)

效果

请添加图片描述
直接将脚本挂载在Text Mesh Pro上,但是需要滚动的文本必须在Scroll View中,否侧会定位错误,还需要给Scroll View中看需求添加垂直或者水平布局的组件

代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;using TMPro;
using UnityEngine.EventSystems;public class TextScroll : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {// TextMeshPro的文本框private TextMeshProUGUI text;private string saveStr; // 保存文本内容,可以保存,但是没有必要,需要实现动态文本private Vector3 savePostion; // 保存文本位置private Vector2 savesizeDelta; // 保存尺寸private float saveMoveWeight;private RectTransform rect; // 文本的方形转换器[Tooltip("开启自动权重")]public bool AutoMoveWeight = true;// 如果开启自动权重那么对该变量修改不在起作用[Tooltip("溢出文本移动的权重,会根据权重的值,来对溢出内容的多少进行加速")]public float moveWeight = 3;private void OnEnable() {text = GetComponent<TextMeshProUGUI>();rect = text.gameObject.GetComponent<RectTransform>();Init();}/// <summary>/// 初始化文本内容/// </summary>public void Init() {saveStr = text.text;savePostion = rect.position;savesizeDelta = rect.sizeDelta;saveMoveWeight = moveWeight;}private Coroutine coroutine; // 接收协程/// <summary>/// 鼠标进入/// </summary>/// <param name="eventData">传入事件数据,鼠标位置等</param>public void OnPointerEnter(PointerEventData eventData) {// Debug.Log("鼠标进入开始文本滚动");// saveStr = text.text;// 是否存在截断float fontsLenght = CalculateTextWidth(text);if (fontsLenght < rect.sizeDelta.x ) return;// 处理上一次退出后未完成恢复完成就再次进入if (coroutine != null) {StopCoroutine(coroutine);Reset();}// 是否启动自动更新if (AutoMoveWeight) {moveWeight = (fontsLenght - rect.sizeDelta.x) / 100;}// 计算所需时间float sumTime = (fontsLenght - rect.sizeDelta.x) / text.fontSize / moveWeight;rect.sizeDelta = new Vector2(fontsLenght + 100, rect.sizeDelta.y);coroutine = StartCoroutine(IETextScroll(sumTime, false));}/// <summary>/// 鼠标移出/// </summary>/// <param name="eventData"></param>public void OnPointerExit(PointerEventData eventData) {// Debug.Log("text begine reset");// 过滤if (text == null || (coroutine == null && CalculateTextWidth(text) < rect.sizeDelta.x) || totalDistance == 0) return;if (coroutine != null) { // 文本正在向左滚动StopCoroutine(coroutine);} coroutine = StartCoroutine(IETextScroll(totalTime / 3, true));// Reset();}/// <summary>/// 计算文本内容宽度/// </summary>/// <param name="text"></param>/// <returns></returns>private float CalculateTextWidth(TextMeshProUGUI text) {float width = text.preferredWidth;return width;}private float totalDistance = 0;private float totalTime = 0;/// <summary>/// 文本滚动的协程/// </summary>/// <param name="time">协程运行时间</param>/// <param name="isReset">是否是恢复时启动的协程</param>/// <returns></returns>private IEnumerator IETextScroll(float time, bool isReset) {// float moveSpeed = 0;float perDistance = 0;if (!isReset) {while (time > 0) {// Time.deltaTime 是一个不确定的量,需要每帧更新。perDistance = moveWeight * text.fontSize * Time.deltaTime;rect.position = new Vector3(rect.position.x - perDistance, rect.position.y);time -= Time.deltaTime;totalDistance += perDistance;totalTime += Time.deltaTime;yield return null;}} else { // 恢复//moveSpeed = totalDistance / time;//while (time > 0) {//    perDistance = moveSpeed * Time.deltaTime;//    rect.position = new Vector3(rect.position.x + perDistance, rect.position.y);//    time -= Time.deltaTime;//    yield return null;//}Reset();}// Debug.Log("移动权重: " + moveWeight + " 每次距离: " + totalDistance + " 花费时间: " + totalTime);yield return null;}/// <summary>/// 恢复/// </summary>private void Reset() {if (text == null) return;// text.text = saveStr;rect.position = savePostion;rect.sizeDelta = savesizeDelta;moveWeight = saveMoveWeight; // 采用自动权重时会再次自动计算权重totalDistance = 0;totalTime = 0;}
}
http://www.lryc.cn/news/179924.html

相关文章:

  • GNN PyG~torch_geometric 学习理解
  • ChatGPT 调教指南:从 PDF 提取标题并保存
  • 【day10.01】使用select实现服务器并发
  • Android修行手册 - Activity 在 Java 和 Kotlin 中怎么写构造参数
  • 【IPC 通信】信号处理接口 Signal API(7)
  • springboot和vue:十二、VueRouter(动态路由)+导航守卫
  • 文心一言 VS 讯飞星火 VS chatgpt (103)-- 算法导论10.1 1题
  • 【ShaderLab罪恶装备卡通角色_二次元风格_“Sol Badguy“_角色渲染(第二篇)】
  • raw智能照片处理工具DxO PureRAW mac介绍
  • 1.centos7 安装显卡驱动、cuda、cudnn
  • WordPress主题开发( 十四)之—— 主题开发示例
  • rust学习-any中的downcast和downcast_ref
  • js检测数据类型总结
  • 获奖作品展示 | 2023嵌入式大赛AidLux系列作品精彩纷呈
  • Mybatis 二级缓存(使用Redis作为二级缓存)
  • VMware vSphere ESXI 6.7 U3封装RTL8125B网卡驱动
  • 黑马JVM总结(二十五)
  • 基础数据结构之——【顺序表】(上)
  • Apollo自动驾驶系统概述(文末参与活动赠送百度周边)
  • Java 21 新特性:Unnamed Classes and Instance Main Methods
  • Tomcat启动后的日志输出为乱码
  • CSP-J第二轮试题-2021年-4题
  • 10.1 今日任务:select实现服务器并发
  • P1540 [NOIP2010 提高组] 机器翻译(模拟)
  • 生信教程:ABBA-BABA分析之滑动窗口
  • 二分答案(求最大值的最小值||求最小值的最大值)
  • 思维模型 周期
  • 从 0 到 1 ,手把手教你编写《消息队列》项目(Java实现) —— 介绍项目/ 需求分析
  • Python学习之索引与切片
  • 编程每日一练(多语言实现)基础篇:满足abcd=(ab+cd)^2的数 (增加Go语言实现)