TextMeshProFloatingText.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class TextMeshProFloatingText : MonoBehaviour
  6. {
  7. public Font TheFont;
  8. private GameObject m_floatingText;
  9. private TextMeshPro m_textMeshPro;
  10. private TextMesh m_textMesh;
  11. private Transform m_transform;
  12. private Transform m_floatingText_Transform;
  13. private Transform m_cameraTransform;
  14. Vector3 lastPOS = Vector3.zero;
  15. Quaternion lastRotation = Quaternion.identity;
  16. public int SpawnType;
  17. //private int m_frame = 0;
  18. void Awake()
  19. {
  20. m_transform = transform;
  21. m_floatingText = new GameObject(this.name + " floating text");
  22. // Reference to Transform is lost when TMP component is added since it replaces it by a RectTransform.
  23. //m_floatingText_Transform = m_floatingText.transform;
  24. //m_floatingText_Transform.position = m_transform.position + new Vector3(0, 15f, 0);
  25. m_cameraTransform = Camera.main.transform;
  26. }
  27. void Start()
  28. {
  29. if (SpawnType == 0)
  30. {
  31. // TextMesh Pro Implementation
  32. m_textMeshPro = m_floatingText.AddComponent<TextMeshPro>();
  33. m_textMeshPro.rectTransform.sizeDelta = new Vector2(3, 3);
  34. m_floatingText_Transform = m_floatingText.transform;
  35. m_floatingText_Transform.position = m_transform.position + new Vector3(0, 15f, 0);
  36. //m_textMeshPro.fontAsset = Resources.Load("Fonts & Materials/JOKERMAN SDF", typeof(TextMeshProFont)) as TextMeshProFont; // User should only provide a string to the resource.
  37. //m_textMeshPro.fontSharedMaterial = Resources.Load("Fonts & Materials/LiberationSans SDF", typeof(Material)) as Material;
  38. m_textMeshPro.alignment = TextAlignmentOptions.Center;
  39. m_textMeshPro.color = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
  40. m_textMeshPro.fontSize = 24;
  41. //m_textMeshPro.enableExtraPadding = true;
  42. //m_textMeshPro.enableShadows = false;
  43. m_textMeshPro.enableKerning = false;
  44. m_textMeshPro.text = string.Empty;
  45. StartCoroutine(DisplayTextMeshProFloatingText());
  46. }
  47. else if (SpawnType == 1)
  48. {
  49. //Debug.Log("Spawning TextMesh Objects.");
  50. m_floatingText_Transform = m_floatingText.transform;
  51. m_floatingText_Transform.position = m_transform.position + new Vector3(0, 15f, 0);
  52. m_textMesh = m_floatingText.AddComponent<TextMesh>();
  53. m_textMesh.font = Resources.Load<Font>("Fonts/ARIAL");
  54. m_textMesh.GetComponent<Renderer>().sharedMaterial = m_textMesh.font.material;
  55. m_textMesh.color = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
  56. m_textMesh.anchor = TextAnchor.LowerCenter;
  57. m_textMesh.fontSize = 24;
  58. StartCoroutine(DisplayTextMeshFloatingText());
  59. }
  60. else if (SpawnType == 2)
  61. {
  62. }
  63. }
  64. //void Update()
  65. //{
  66. // if (SpawnType == 0)
  67. // {
  68. // m_textMeshPro.SetText("{0}", m_frame);
  69. // }
  70. // else
  71. // {
  72. // m_textMesh.text = m_frame.ToString();
  73. // }
  74. // m_frame = (m_frame + 1) % 1000;
  75. //}
  76. public IEnumerator DisplayTextMeshProFloatingText()
  77. {
  78. float CountDuration = 2.0f; // How long is the countdown alive.
  79. float starting_Count = Random.Range(5f, 20f); // At what number is the counter starting at.
  80. float current_Count = starting_Count;
  81. Vector3 start_pos = m_floatingText_Transform.position;
  82. Color32 start_color = m_textMeshPro.color;
  83. float alpha = 255;
  84. int int_counter = 0;
  85. float fadeDuration = 3 / starting_Count * CountDuration;
  86. while (current_Count > 0)
  87. {
  88. current_Count -= (Time.deltaTime / CountDuration) * starting_Count;
  89. if (current_Count <= 3)
  90. {
  91. //Debug.Log("Fading Counter ... " + current_Count.ToString("f2"));
  92. alpha = Mathf.Clamp(alpha - (Time.deltaTime / fadeDuration) * 255, 0, 255);
  93. }
  94. int_counter = (int)current_Count;
  95. m_textMeshPro.text = int_counter.ToString();
  96. //m_textMeshPro.SetText("{0}", (int)current_Count);
  97. m_textMeshPro.color = new Color32(start_color.r, start_color.g, start_color.b, (byte)alpha);
  98. // Move the floating text upward each update
  99. m_floatingText_Transform.position += new Vector3(0, starting_Count * Time.deltaTime, 0);
  100. // Align floating text perpendicular to Camera.
  101. if (!lastPOS.Compare(m_cameraTransform.position, 1000) || !lastRotation.Compare(m_cameraTransform.rotation, 1000))
  102. {
  103. lastPOS = m_cameraTransform.position;
  104. lastRotation = m_cameraTransform.rotation;
  105. m_floatingText_Transform.rotation = lastRotation;
  106. Vector3 dir = m_transform.position - lastPOS;
  107. m_transform.forward = new Vector3(dir.x, 0, dir.z);
  108. }
  109. yield return new WaitForEndOfFrame();
  110. }
  111. //Debug.Log("Done Counting down.");
  112. yield return new WaitForSeconds(Random.Range(0.1f, 1.0f));
  113. m_floatingText_Transform.position = start_pos;
  114. StartCoroutine(DisplayTextMeshProFloatingText());
  115. }
  116. public IEnumerator DisplayTextMeshFloatingText()
  117. {
  118. float CountDuration = 2.0f; // How long is the countdown alive.
  119. float starting_Count = Random.Range(5f, 20f); // At what number is the counter starting at.
  120. float current_Count = starting_Count;
  121. Vector3 start_pos = m_floatingText_Transform.position;
  122. Color32 start_color = m_textMesh.color;
  123. float alpha = 255;
  124. int int_counter = 0;
  125. float fadeDuration = 3 / starting_Count * CountDuration;
  126. while (current_Count > 0)
  127. {
  128. current_Count -= (Time.deltaTime / CountDuration) * starting_Count;
  129. if (current_Count <= 3)
  130. {
  131. //Debug.Log("Fading Counter ... " + current_Count.ToString("f2"));
  132. alpha = Mathf.Clamp(alpha - (Time.deltaTime / fadeDuration) * 255, 0, 255);
  133. }
  134. int_counter = (int)current_Count;
  135. m_textMesh.text = int_counter.ToString();
  136. //Debug.Log("Current Count:" + current_Count.ToString("f2"));
  137. m_textMesh.color = new Color32(start_color.r, start_color.g, start_color.b, (byte)alpha);
  138. // Move the floating text upward each update
  139. m_floatingText_Transform.position += new Vector3(0, starting_Count * Time.deltaTime, 0);
  140. // Align floating text perpendicular to Camera.
  141. if (!lastPOS.Compare(m_cameraTransform.position, 1000) || !lastRotation.Compare(m_cameraTransform.rotation, 1000))
  142. {
  143. lastPOS = m_cameraTransform.position;
  144. lastRotation = m_cameraTransform.rotation;
  145. m_floatingText_Transform.rotation = lastRotation;
  146. Vector3 dir = m_transform.position - lastPOS;
  147. m_transform.forward = new Vector3(dir.x, 0, dir.z);
  148. }
  149. yield return new WaitForEndOfFrame();
  150. }
  151. //Debug.Log("Done Counting down.");
  152. yield return new WaitForSeconds(Random.Range(0.1f, 1.0f));
  153. m_floatingText_Transform.position = start_pos;
  154. StartCoroutine(DisplayTextMeshFloatingText());
  155. }
  156. }
  157. }