SideTipView.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using JCUnityLib;
  6. public class SideTipView : ViewBase
  7. {
  8. Transform container;
  9. GameObject sideTipPrefab;
  10. static SideTipView ins;
  11. void Awake()
  12. {
  13. ins = this;
  14. container = transform.Find("Container");
  15. sideTipPrefab = container.Find("SideTip").gameObject;
  16. sideTipPrefab.SetActive(false);
  17. }
  18. void OnDestroy()
  19. {
  20. if (ins == this) ins = null;
  21. }
  22. void _ShowTip(string text, Color color)
  23. {
  24. var o = Instantiate(sideTipPrefab);
  25. var t = o.GetComponentInChildren<Text>();
  26. t.text = text;
  27. t.color = color;
  28. o.transform.SetParent(container);
  29. o.SetActive(true);
  30. StartCoroutine(AutoDestroy(o));
  31. }
  32. IEnumerator AutoDestroy(GameObject o)
  33. {
  34. yield return new WaitForSeconds(5.0f);
  35. Destroy(o);
  36. }
  37. public static void ShowTip(string text, Color color)
  38. {
  39. if (!ins) Instantiate(Resources.Load("Prefabs/Views/SideTipView"));
  40. ins?._ShowTip(text, color);
  41. }
  42. }