TipText.cs 789 B

12345678910111213141516171819202122232425262728293031323334
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. public class TipText : MonoBehaviour
  5. {
  6. private static TipText _Instance;
  7. void Awake()
  8. {
  9. _Instance = this;
  10. gameObject.SetActive(false);
  11. }
  12. void Notify(string text)
  13. {
  14. gameObject.SetActive(true);
  15. GetComponentInChildren<Text>().text = text;
  16. LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
  17. StopCoroutine("DelayHide");
  18. StartCoroutine("DelayHide");
  19. }
  20. IEnumerator DelayHide()
  21. {
  22. yield return new WaitForSeconds(3);
  23. gameObject.SetActive(false);
  24. }
  25. public static void Show(string text)
  26. {
  27. if (_Instance) _Instance.Notify("<color=#2FFFEC>提示</color>\n" + text);
  28. }
  29. }