using System.Collections;
using TMPro;
using UnityEngine;
[RequireComponent(typeof(TextMeshProUGUI))]
public class TextShake : MonoBehaviour
{
[SerializeField]
private float _interval = 0.5f;
[SerializeField]
private float _maxOffset = 10f;
private IEnumerator Start()
{
var component = GetComponent<TMP_Text>();
component.ForceMeshUpdate();
var textInfo = component.textInfo;
var cachedMeshInfo = textInfo.CopyMeshInfoVertexData();
var cc = textInfo.characterCount;
while (true)
{
for (var characterIndex = 0; characterIndex < cc; characterIndex++)
{
var charInfo = textInfo.characterInfo[characterIndex];
if (!charInfo.isVisible)
continue;
var materialIndex = charInfo.materialReferenceIndex;
var vertexIndex = charInfo.vertexIndex;
var sourceVertices = cachedMeshInfo[materialIndex].vertices;
var destinationVertices = textInfo.meshInfo[materialIndex].vertices;
for (var i = 0; i < 4; i++)
{
var index = vertexIndex + i;
for (var j = 0; j < 2; j++)
{
destinationVertices[index][j] = sourceVertices[index][j] + Random.Range(-_maxOffset, _maxOffset);
}
}
}
for (var i = 0; i < textInfo.meshInfo.Length; i++)
{
textInfo.meshInfo[i].mesh.vertices = textInfo.meshInfo[i].vertices;
component.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
}
yield return new WaitForSeconds(_interval);
}
}
}