TextConsoleSimulator.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class TextConsoleSimulator : MonoBehaviour
  6. {
  7. private TMP_Text m_TextComponent;
  8. private bool hasTextChanged;
  9. void Awake()
  10. {
  11. m_TextComponent = gameObject.GetComponent<TMP_Text>();
  12. }
  13. void Start()
  14. {
  15. StartCoroutine(RevealCharacters(m_TextComponent));
  16. //StartCoroutine(RevealWords(m_TextComponent));
  17. }
  18. void OnEnable()
  19. {
  20. // Subscribe to event fired when text object has been regenerated.
  21. TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
  22. }
  23. void OnDisable()
  24. {
  25. TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
  26. }
  27. // Event received when the text object has changed.
  28. void ON_TEXT_CHANGED(Object obj)
  29. {
  30. hasTextChanged = true;
  31. }
  32. /// <summary>
  33. /// Method revealing the text one character at a time.
  34. /// </summary>
  35. /// <returns></returns>
  36. IEnumerator RevealCharacters(TMP_Text textComponent)
  37. {
  38. textComponent.ForceMeshUpdate();
  39. TMP_TextInfo textInfo = textComponent.textInfo;
  40. int totalVisibleCharacters = textInfo.characterCount; // Get # of Visible Character in text object
  41. int visibleCount = 0;
  42. while (true)
  43. {
  44. if (hasTextChanged)
  45. {
  46. totalVisibleCharacters = textInfo.characterCount; // Update visible character count.
  47. hasTextChanged = false;
  48. }
  49. if (visibleCount > totalVisibleCharacters)
  50. {
  51. yield return new WaitForSeconds(1.0f);
  52. visibleCount = 0;
  53. }
  54. textComponent.maxVisibleCharacters = visibleCount; // How many characters should TextMeshPro display?
  55. visibleCount += 1;
  56. yield return null;
  57. }
  58. }
  59. /// <summary>
  60. /// Method revealing the text one word at a time.
  61. /// </summary>
  62. /// <returns></returns>
  63. IEnumerator RevealWords(TMP_Text textComponent)
  64. {
  65. textComponent.ForceMeshUpdate();
  66. int totalWordCount = textComponent.textInfo.wordCount;
  67. int totalVisibleCharacters = textComponent.textInfo.characterCount; // Get # of Visible Character in text object
  68. int counter = 0;
  69. int currentWord = 0;
  70. int visibleCount = 0;
  71. while (true)
  72. {
  73. currentWord = counter % (totalWordCount + 1);
  74. // Get last character index for the current word.
  75. if (currentWord == 0) // Display no words.
  76. visibleCount = 0;
  77. else if (currentWord < totalWordCount) // Display all other words with the exception of the last one.
  78. visibleCount = textComponent.textInfo.wordInfo[currentWord - 1].lastCharacterIndex + 1;
  79. else if (currentWord == totalWordCount) // Display last word and all remaining characters.
  80. visibleCount = totalVisibleCharacters;
  81. textComponent.maxVisibleCharacters = visibleCount; // How many characters should TextMeshPro display?
  82. // Once the last character has been revealed, wait 1.0 second and start over.
  83. if (visibleCount >= totalVisibleCharacters)
  84. {
  85. yield return new WaitForSeconds(1.0f);
  86. }
  87. counter += 1;
  88. yield return new WaitForSeconds(0.1f);
  89. }
  90. }
  91. }
  92. }