Global Game Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
8.9 KiB

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