赞
踩
用sin函数来表示
- using UnityEngine;
- using TMPro;
- using System.Collections;
-
- public class WaveTextAnimation : MonoBehaviour
- {
- public float amplitude = 0.3f;
- public float frequency = 2f;
-
- private TMP_Text textComponent;
- private string originalText;
-
- private void Start()
- {
- textComponent = GetComponent<TMP_Text>();
- originalText = textComponent.text;
- }
-
-
- private void Update()
- {
- // 根据时间计算波浪形的偏移量
- float offset = Mathf.Sin(Time.time * frequency) * amplitude;
-
- // 应用波浪形偏移量到每个字符
- for (int i = 0; i < originalText.Length; i++)
- {
- TMP_CharacterInfo charInfo = textComponent.textInfo.characterInfo[i];
-
- // 跳过空格和换行符
- if (!char.IsWhiteSpace(originalText[i]))
- {
- int materialIndex = charInfo.materialReferenceIndex;
- int vertexIndex = charInfo.vertexIndex;
-
- // 计算每个字符的顶点偏移
- Vector3[] vertices = textComponent.textInfo.meshInfo[materialIndex].vertices;
- for (int j = 0; j < 4; j++)
- {
- // vertices[vertexIndex + j] += new Vector3(0f, offset, 0f);
- var orig = vertices[vertexIndex + j];
-
- vertices[vertexIndex + j]=orig+new Vector3(0, Mathf.Sin(Time.time*frequency+orig.x*frequency)*amplitude, 0);
- }
- }
- }
-
- // 更新文本网格的顶点信息
- for (int i = 0; i < textComponent.textInfo.meshInfo.Length; i++)
- {
- textComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.All);
- }
- }
-
-
- }
文本上下移动
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class TextWaveMotion : MonoBehaviour
- {
- public float amplitude = 1f;
- public float frequency = 1f;
-
- private float startTime;
-
- private void Start()
- {
- startTime = Time.time;
- }
-
- private void Update()
- {
- // 计算波浪形变化
- float waveValue = Mathf.Sin((Time.time - startTime) * frequency) * amplitude;
-
- // 应用波浪形变化到文本对象的位置
- transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y+waveValue, transform.localPosition.z);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。