MessageDisplay.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /// <summary>
  6. /// Changes the text on an attached TextMesh and/or UI.Text to the specified message, with static methods
  7. /// for sending the message to all displays at once. Also supports temporary messages.
  8. /// Used in the ZED MR Calibration scene to update the instructions displays when they need to change.
  9. /// </summary>
  10. public class MessageDisplay : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// All registered instances of MessageDisplay.
  14. /// </summary>
  15. public static List<MessageDisplay> instances = new List<MessageDisplay>();
  16. /// <summary>
  17. /// Update every MessageDisplay with the provided message so long as it has been enabled.
  18. /// </summary>
  19. /// <param name="message"></param>
  20. public static void DisplayMessageAll(string message)
  21. {
  22. foreach(MessageDisplay instance in instances)
  23. {
  24. instance.DisplayMessage(message);
  25. }
  26. lastMessage = message;
  27. }
  28. /// <summary>
  29. /// The last message called with DisplayMessageAll.
  30. /// Used to make sure newly instantiated MessageDisplays set their message to the correct one.
  31. /// </summary>
  32. internal static string lastMessage = "";
  33. /// <summary>
  34. /// Displays a message on every MessageDisplay for a specified amount of time.
  35. /// Afterwards, it reverts to the message set before it.
  36. /// </summary>
  37. public static void DisplayTemporaryMessageAll(string message, float durationseconds = 7f)
  38. {
  39. foreach(MessageDisplay instance in instances)
  40. {
  41. instance.StartCoroutine(instance.DisplayTempMessage(message, durationseconds));
  42. }
  43. }
  44. /// <summary>
  45. /// Sets all MessageDisplays to an empty string.
  46. /// </summary>
  47. public static void ClearAll()
  48. {
  49. foreach (MessageDisplay instance in instances)
  50. {
  51. instance.Clear();
  52. }
  53. lastMessage = "";
  54. }
  55. /// <summary>
  56. /// Optional 2D UI Text object to update when it receives messages.
  57. /// </summary>
  58. [Tooltip("Optional 2D UI Text object to update when it receives messages.")]
  59. public Text text2D;
  60. /// <summary>
  61. /// Optional 3D Text object to update when it receives messages.
  62. /// </summary>
  63. [Tooltip("Optional 3D Text object to update when it receives messages.")]
  64. public TextMesh text3D;
  65. /// <summary>
  66. /// Color to temporarily set the text color to when updating to a temporary message.
  67. /// </summary>
  68. [Space(5)]
  69. [Tooltip("Color to temporarily set the text color to when updating to a temporary message.")]
  70. public Color tempMessageTextColor = Color.yellow;
  71. private string currentMessage;
  72. /// <summary>
  73. /// Registers this instance to be affected when the static message update methods are called,
  74. /// and updates itself based on the most recently updated message in case it was enabled late.
  75. /// </summary>
  76. void Awake ()
  77. {
  78. instances.Add(this);
  79. DisplayMessage(lastMessage);
  80. }
  81. /// <summary>
  82. /// Changes the attached text object(s) to read the specified message.
  83. /// </summary>
  84. /// <param name="message"></param>
  85. public void DisplayMessage(string message)
  86. {
  87. if(text2D != null)
  88. {
  89. text2D.text = message;
  90. }
  91. if(text3D != null)
  92. {
  93. text3D.text = message;
  94. }
  95. currentMessage = message;
  96. }
  97. /// <summary>
  98. /// Sets the attached text object(s) to an empty string.
  99. /// </summary>
  100. public void Clear()
  101. {
  102. DisplayMessage("");
  103. }
  104. private void OnDestroy()
  105. {
  106. instances.Remove(this);
  107. }
  108. /// <summary>
  109. /// Temporarily changes the message to the one provided, along with its color, before reverting back
  110. /// to whatever message appeared before it.
  111. /// </summary>
  112. private IEnumerator DisplayTempMessage(string message, float durationseconds = 3f)
  113. {
  114. //Cache old message and text colors.
  115. string oldmessage = currentMessage;
  116. Color normalTextColor2D = Color.white;
  117. Color normalTextColor3D = Color.white;
  118. if (text2D) normalTextColor2D = text2D.color;
  119. if (text3D) normalTextColor3D = text3D.color;
  120. //Print the message.
  121. DisplayMessage(message);
  122. //Change colors to temporary message color to make it stand out more.
  123. if (text2D) text2D.color = tempMessageTextColor;
  124. if (text3D) text3D.color = tempMessageTextColor;
  125. for (float t = 0; t < durationseconds; t += Time.deltaTime)
  126. {
  127. yield return null;
  128. }
  129. //Restore the original message and text colors.
  130. DisplayMessage(oldmessage);
  131. if (text2D) text2D.color = normalTextColor2D;
  132. if (text3D) text3D.color = normalTextColor3D;
  133. }
  134. }