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

chrome浏览器官网入口搜索引擎营销seo

chrome浏览器官网入口,搜索引擎营销seo,谷歌云可以做网站吗,哪里网站建设公司比较好Unity TextMeshPro 实现文本逐字淡出效果 前言项目思路场景布置代码编写 前言 在处理角色对话时经常会用到一些文本动画,正好记录一下。使用 TextMeshPro,我们可以直接操作文本的顶点数据,实现诸如渐变、动画等效果,为游戏界面和…

Unity TextMeshPro 实现文本逐字淡出效果

  • 前言
  • 项目
    • 思路
    • 场景布置
    • 代码编写

前言

在处理角色对话时经常会用到一些文本动画,正好记录一下。使用 TextMeshPro,我们可以直接操作文本的顶点数据,实现诸如渐变、动画等效果,为游戏界面和应用程序增添动感。
逐字淡出效果

项目

思路

实现文字缓慢出现的关键在于:

  1. 初始状态设置
    在文字显示前,将所有字符的顶点颜色透明度(Alpha)设为 0,确保文本初始完全不可见。

  2. 逐字符渐显
    利用协程逐个为每个字符开启渐变效果,缓慢将透明度从 0 过渡到 255。这里需要注意:

  • 避免在渐显过程中频繁调用 ForceMeshUpdate(),因为每次调用都会重置网格数据,可能导致其他字符状态被覆盖。
  • 预先缓存目标字符的材质索引、顶点索引和颜色数组,确保只修改目标字符的数据。
  1. 网格数据同步
    每次修改完顶点颜色后,需要将颜色数组重新应用到网格上,并调用 UpdateVertexData() 来刷新显示。

场景布置

场景截图

代码编写

using UnityEngine;
using TMPro;
using System.Collections;
using UnityEngine.PlayerLoop;public class TextFadeIn : MonoBehaviour
{public float fadeDuration = 0.5f; // 每个字符的渐变时间public float interval = 0.1f;     // 字符之间的间隔时间public TMP_Text textComponent;public string originalText;private void Update(){if (Input.GetKeyDown(KeyCode.Space)){ShowTextAnim("The key is not to re enable automatic mesh generation after modifying the vertex color. Instead, set the required properties first, generate the mesh, and finally modify the vertex color to ensure that the mesh is not reset after manual modification.");}}public void ShowTextAnim(string txtString){StopAllCoroutines();textComponent.text = "";originalText = txtString;StartCoroutine(DelayedStart());}IEnumerator DelayedStart(){// 先设置好文本和属性,启用 word wrapping(如果需要)textComponent.enableWordWrapping = true;textComponent.text = originalText;// 生成网格数据,此时网格数据已经包含 word wrapping 的效果textComponent.ForceMeshUpdate();// 获取最新的文本信息TMP_TextInfo textInfo = textComponent.textInfo;// 将所有可见字符的顶点颜色的 alpha 设置为 0(透明)for (int i = 0; i < textInfo.characterCount; i++){if (!textInfo.characterInfo[i].isVisible)continue;int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;int vertexIndex = textInfo.characterInfo[i].vertexIndex;Color32[] vertexColors = textInfo.meshInfo[materialIndex].colors32;for (int j = 0; j < 4; j++){vertexColors[vertexIndex + j].a = 0;}}// 应用顶点颜色更改到网格for (int i = 0; i < textInfo.meshInfo.Length; i++){textInfo.meshInfo[i].mesh.colors32 = textInfo.meshInfo[i].colors32;}textComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);// 等待一帧确保更改已生效yield return null;// 开始字符渐入效果StartCoroutine(ShowText());}IEnumerator ShowText(){TMP_TextInfo textInfo = textComponent.textInfo;int totalCharacters = textInfo.characterCount;// 逐个启动字符渐显协程(顺序进行)for (int i = 0; i < totalCharacters; i++){if (textInfo.characterInfo[i].isVisible){// 等待当前字符渐显完成后再处理下一个字符yield return StartCoroutine(FadeCharacter(i));yield return new WaitForSeconds(interval);}}}IEnumerator FadeCharacter(int characterIndex){TMP_TextInfo textInfo = textComponent.textInfo;if (characterIndex >= textInfo.characterCount || !textInfo.characterInfo[characterIndex].isVisible)yield break;// 缓存目标字符的相关信息TMP_CharacterInfo charInfo = textInfo.characterInfo[characterIndex];int materialIndex = charInfo.materialReferenceIndex;int vertexIndex = charInfo.vertexIndex;Color32[] vertexColors = textInfo.meshInfo[materialIndex].colors32;float elapsedTime = 0f;while (elapsedTime < fadeDuration){elapsedTime += Time.deltaTime;float alpha = Mathf.Clamp01(elapsedTime / fadeDuration);byte alphaByte = (byte)(alpha * 255);// 仅更新目标字符的顶点颜色for (int j = 0; j < 4; j++){vertexColors[vertexIndex + j].a = alphaByte;}// 将更新后的颜色数组直接应用到对应网格textInfo.meshInfo[materialIndex].mesh.colors32 = vertexColors;textComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);yield return null;}}private void OnDisable(){StopAllCoroutines();}
}
http://www.dtcms.com/wzjs/507555.html

相关文章:

  • 深圳php网站建设厦门人才网最新招聘信息网
  • 上海seo网站优化湖南关键词优化排名推广
  • 网站建设外包行业优化大师官方免费
  • iis 网站名热门搜索排行榜
  • 遂宁公司做网站企业网上的推广
  • 中英互译网站怎么做搜索网站哪个好
  • 四川省建设厅电子政务网站谷歌关键词挖掘工具
  • 公司网站费用计入什么科目电脑课程培训零基础
  • 设计本网站图片大全怎么样做推广
  • 网站设计报价方案windows优化大师下载安装
  • 抚州网站建设公司手机关键词排名优化
  • 网站设计师接单seo查询优化
  • 南京手机网站制作百度关键字
  • 男女做暖网站是什么样子的外链发布平台
  • 做网站号码互联网销售怎么做
  • 有关建设旅行网站的建设广州网站优化推广方案
  • 刷赞网站怎么做的昆明seo
  • 怎样做免费外贸网站营销策划方案怎么写
  • 唯独建设银行网站无法登陆2023年6月份又封城了
  • 广东中国移动网站如何在百度上做产品推广
  • 熵网站站长工具pr值查询
  • 自己做坑人网站的软件武汉网站制作推广
  • 奎文营销型网站建设百度一下就知道百度首页
  • 免费ppt课件下载网站营销策划方案范文
  • 如何使用网站模板建设网站优化是什么梗
  • 网站在线客服公众号怎么做江门网站建设
  • 太原建设局网站谷歌官方网站注册
  • 小区网站开发论文平台广告推广
  • 网站维护需要哪些知识昆明网站seo服务
  • 可以自己做安卓app的网站推广软文是什么