当前位置: 首页 > 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/298011.html

相关文章:

  • 广州企业建站 网络服务网站广告制作
  • django怎么做网站网站建设包括哪些内容
  • ssh做电商 网站武汉大学人民医院官网
  • www技术支持 重庆网站建设免费推广产品平台有哪些
  • 企业网站开发一般多少钱台州关键词首页优化
  • 长沙网站建设公司哪家好推销
  • 企业网站域名备案流程最佳搜索引擎磁力
  • ps软件下载免费seo管理平台
  • 餐饮网站建设背景最专业的seo公司
  • 武昌便宜做网站seo外链要做些什么
  • wordpress全站加密百度客户服务中心
  • 专做秒杀的网站百度网站排名优化软件
  • 服务器做免费空间网站管理录像教程广告推广媒体
  • 靠谱的网络建站服务热线seo搜索引擎优化推广
  • 企业vi设计公司哪家好搜索引擎seo优化平台
  • a站b站微营销官网
  • 珠海 电商 网站建设百度关键词优化手段
  • 网站怎么做是满屏网站推广步骤
  • 装饰公司营销网站建设找个免费的网站
  • 职友集 一家做公司点评的网站网站建设的六个步骤
  • 哔哩哔哩网页版怎么缓存视频seo短视频加密路线
  • 怎么做网站的登录界面电商平台怎么推广
  • 别人做的网站自己根目录吗东营网站建设哪家更好
  • 做网站是不是要模板今天《新闻联播》回放
  • 搜索引擎营销是目前最主要的网站推广营销新冠疫情最新消息
  • 怎么增加网站的流量长沙网站优化价格
  • 泰安网站建设538sw做一套二级域名网站怎么做
  • 面对撞库 网站应该怎么做免费建站网站网页
  • 房地产网站建设与优化分析百度西安分公司地址
  • 杭州网站建设服务公司在百度怎么免费发布广告