SideTipView.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. GameObject sideTip;
  9. Text sideTipText;
  10. List<string> tipStrList = new List<string>();
  11. static SideTipView ins;
  12. void Awake()
  13. {
  14. ins = this;
  15. sideTip = transform.Find("SideTip").gameObject;
  16. sideTipText = sideTip.GetComponentInChildren<Text>();
  17. }
  18. void OnDestroy()
  19. {
  20. if (ins == this) ins = null;
  21. }
  22. void _ShowTip(string text, Color color)
  23. {
  24. string c = "white";
  25. if (color == Color.yellow) c = "yellow";
  26. string s =
  27. $"<color=#BBFFFF>{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}</color>\n" +
  28. $"<color={c}>{text}</color>";
  29. tipStrList.Add(s);
  30. sideTipText.text = string.Join("\n", tipStrList);
  31. if (tipStrList.Count == 1) sideTip.SetActive(true);
  32. StartCoroutine(AutoDestroy());
  33. }
  34. IEnumerator AutoDestroy()
  35. {
  36. yield return new WaitForSeconds(10.0f);
  37. tipStrList.RemoveAt(0);
  38. sideTipText.text = string.Join("\n", tipStrList);
  39. if (tipStrList.Count == 0) sideTip.SetActive(false);
  40. }
  41. public static void ShowTip(string text, Color color)
  42. {
  43. Debug.Log(text);
  44. // if (!ins) Instantiate(Resources.Load("Prefabs/Views/SideTipView"));
  45. // ins?._ShowTip(text, color);
  46. }
  47. }