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

商城首页网站网上营销新观察网

商城首页网站,网上营销新观察网,网站建设新趋势,如何用微信公众号做企业网站效果 直接将脚本挂载在Text Mesh Pro上,但是需要滚动的文本必须在Scroll View中,否侧会定位错误,还需要给Scroll View中看需求添加垂直或者水平布局的组件 代码 using System.Collections; using System.Collections.Generic; using UnityE…

效果

请添加图片描述
直接将脚本挂载在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.15wanjia.com/news/54049.html

相关文章:

  • 项目管理软件模块济南seo优化公司
  • 网站开发和优化关系公司网站建设需要注意什么
  • 越南人一般去哪个网站做贸易站长工具视频
  • 阳江市住房和城乡规划建设局网站产品推广广告
  • 备案用网站建设方案书新闻最新头条10条
  • 做网站怎么办营业执照百度投放广告一天多少钱
  • 网站服务器租用时间免费seo优化工具
  • 百度搜索站长平台聚名网域名注册
  • 音乐网站建设视频教程中小企业网络推广
  • 网站开发小工具百度广告推广
  • 移动端网站怎么做seo买域名要多少钱一个
  • 2023年做网站怎么样百度小程序优化排名
  • 做3d效果的网站最全bt搜索引擎
  • 深圳seo网站排名优化搜索引擎优化规则
  • 营销活动策划公司百度词条优化工作
  • 技术支持骏域建设网站宁波seo关键词费用
  • 黑龙江省建设厅网站站长揭阳市seo上词外包
  • 辽源网站建设公司媒体资源网官网
  • 跨境电商独立站建站服务最近国家新闻
  • 网站无障碍建设报告seo 的作用和意义
  • 网站搭建用什么软件百度数据中心
  • 网站备案号查电话号码利搜网站排名软件
  • 网站建设 柳州惠州seo快速排名
  • 杭州哪家网站建设公司好seowhy论坛
  • 如何撰写网站建设方案书宁波seo外包推广渠道
  • 政府网站建设与对策分析微信公众号平台官网
  • 扬州网站建设企业淘宝关键词排名是怎么做的
  • 个人做门户网站需要注册文章代写
  • 慈溪网站制作专门看网站的浏览器
  • 福州做网站的公司多少钱刷推广链接人数的软件