TMP_SpriteAnimator.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using UnityEngine;
  2. using UnityEngine.TextCore;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace TMPro
  6. {
  7. [DisallowMultipleComponent]
  8. public class TMP_SpriteAnimator : MonoBehaviour
  9. {
  10. private Dictionary<int, bool> m_animations = new Dictionary<int, bool>(16);
  11. //private bool isPlaying = false;
  12. private TMP_Text m_TextComponent;
  13. void Awake()
  14. {
  15. m_TextComponent = GetComponent<TMP_Text>();
  16. }
  17. void OnEnable()
  18. {
  19. //m_playAnimations = true;
  20. }
  21. void OnDisable()
  22. {
  23. //m_playAnimations = false;
  24. }
  25. public void StopAllAnimations()
  26. {
  27. StopAllCoroutines();
  28. m_animations.Clear();
  29. }
  30. public void DoSpriteAnimation(int currentCharacter, TMP_SpriteAsset spriteAsset, int start, int end, int framerate)
  31. {
  32. // Need to add tracking of coroutines that have been lunched for this text object.
  33. if (!m_animations.TryGetValue(currentCharacter, out bool isPlaying))
  34. {
  35. StartCoroutine(DoSpriteAnimationInternal(currentCharacter, spriteAsset, start, end, framerate));
  36. m_animations.Add(currentCharacter, true);
  37. }
  38. }
  39. IEnumerator DoSpriteAnimationInternal(int currentCharacter, TMP_SpriteAsset spriteAsset, int start, int end, int framerate)
  40. {
  41. if (m_TextComponent == null) yield break;
  42. // We yield otherwise this gets called before the sprite has rendered.
  43. yield return null;
  44. int currentFrame = start;
  45. // Make sure end frame does not exceed the number of sprites in the sprite asset.
  46. if (end > spriteAsset.spriteCharacterTable.Count)
  47. end = spriteAsset.spriteCharacterTable.Count - 1;
  48. // Get a reference to the geometry of the current character.
  49. TMP_CharacterInfo charInfo = m_TextComponent.textInfo.characterInfo[currentCharacter];
  50. int materialIndex = charInfo.materialReferenceIndex;
  51. int vertexIndex = charInfo.vertexIndex;
  52. TMP_MeshInfo meshInfo = m_TextComponent.textInfo.meshInfo[materialIndex];
  53. float elapsedTime = 0;
  54. float targetTime = 1f / Mathf.Abs(framerate);
  55. while (true)
  56. {
  57. if (elapsedTime > targetTime)
  58. {
  59. elapsedTime = 0;
  60. // Get a reference to the current sprite
  61. TMP_SpriteCharacter spriteCharacter = spriteAsset.spriteCharacterTable[currentFrame];
  62. // Update the vertices for the new sprite
  63. Vector3[] vertices = meshInfo.vertices;
  64. Vector2 origin = new Vector2(charInfo.origin, charInfo.baseLine);
  65. float spriteScale = charInfo.fontAsset.faceInfo.ascentLine / spriteCharacter.glyph.metrics.height * spriteCharacter.scale * charInfo.scale;
  66. Vector3 bl = new Vector3(origin.x + spriteCharacter.glyph.metrics.horizontalBearingX * spriteScale, origin.y + (spriteCharacter.glyph.metrics.horizontalBearingY - spriteCharacter.glyph.metrics.height) * spriteScale);
  67. Vector3 tl = new Vector3(bl.x, origin.y + spriteCharacter.glyph.metrics.horizontalBearingY * spriteScale);
  68. Vector3 tr = new Vector3(origin.x + (spriteCharacter.glyph.metrics.horizontalBearingX + spriteCharacter.glyph.metrics.width) * spriteScale, tl.y);
  69. Vector3 br = new Vector3(tr.x, bl.y);
  70. vertices[vertexIndex + 0] = bl;
  71. vertices[vertexIndex + 1] = tl;
  72. vertices[vertexIndex + 2] = tr;
  73. vertices[vertexIndex + 3] = br;
  74. // Update the UV to point to the new sprite
  75. Vector2[] uvs0 = meshInfo.uvs0;
  76. Vector2 uv0 = new Vector2((float)spriteCharacter.glyph.glyphRect.x / spriteAsset.spriteSheet.width, (float)spriteCharacter.glyph.glyphRect.y / spriteAsset.spriteSheet.height);
  77. Vector2 uv1 = new Vector2(uv0.x, (float)(spriteCharacter.glyph.glyphRect.y + spriteCharacter.glyph.glyphRect.height) / spriteAsset.spriteSheet.height);
  78. Vector2 uv2 = new Vector2((float)(spriteCharacter.glyph.glyphRect.x + spriteCharacter.glyph.glyphRect.width) / spriteAsset.spriteSheet.width, uv1.y);
  79. Vector2 uv3 = new Vector2(uv2.x, uv0.y);
  80. uvs0[vertexIndex + 0] = uv0;
  81. uvs0[vertexIndex + 1] = uv1;
  82. uvs0[vertexIndex + 2] = uv2;
  83. uvs0[vertexIndex + 3] = uv3;
  84. // Update the modified vertex attributes
  85. meshInfo.mesh.vertices = vertices;
  86. meshInfo.mesh.uv = uvs0;
  87. m_TextComponent.UpdateGeometry(meshInfo.mesh, materialIndex);
  88. if (framerate > 0)
  89. {
  90. if (currentFrame < end)
  91. currentFrame += 1;
  92. else
  93. currentFrame = start;
  94. }
  95. else
  96. {
  97. if (currentFrame > start)
  98. currentFrame -= 1;
  99. else
  100. currentFrame = end;
  101. }
  102. }
  103. elapsedTime += Time.deltaTime;
  104. yield return null;
  105. }
  106. }
  107. }
  108. }