IncrementUIText.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine.UI;
  2. namespace UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets
  3. {
  4. /// <summary>
  5. /// Add this component to a GameObject and call the <see cref="IncrementText"/> method
  6. /// in response to a Unity Event to update a text display to count up with each event.
  7. /// </summary>
  8. public class IncrementUIText : MonoBehaviour
  9. {
  10. [SerializeField]
  11. [Tooltip("The Text component this behavior uses to display the incremented value.")]
  12. Text m_Text;
  13. /// <summary>
  14. /// The Text component this behavior uses to display the incremented value.
  15. /// </summary>
  16. public Text text
  17. {
  18. get => m_Text;
  19. set => m_Text = value;
  20. }
  21. int m_Count;
  22. /// <summary>
  23. /// See <see cref="MonoBehaviour"/>.
  24. /// </summary>
  25. protected void Awake()
  26. {
  27. if (m_Text == null)
  28. Debug.LogWarning("Missing required Text component reference. Use the Inspector window to assign which Text component to increment.", this);
  29. }
  30. /// <summary>
  31. /// Increment the string message of the Text component.
  32. /// </summary>
  33. public void IncrementText()
  34. {
  35. m_Count += 1;
  36. if (m_Text != null)
  37. m_Text.text = m_Count.ToString();
  38. }
  39. }
  40. }