TipPanel.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace Rokid.UXR.UI
  5. {
  6. public enum TipLevel
  7. {
  8. Normal,
  9. Warning,
  10. Error
  11. }
  12. public class TipPanel : BasePanel, IDialog
  13. {
  14. [SerializeField, Autowrited]
  15. private Image normal;
  16. [SerializeField, Autowrited]
  17. private Image warning;
  18. [SerializeField, Autowrited]
  19. private Image error;
  20. [SerializeField, Autowrited]
  21. private Text tipText;
  22. private float elapsedTime = 0;
  23. private float showTime;
  24. private Action finishCallBack;
  25. public void Init(string msg, TipLevel level, float showTime)
  26. {
  27. Init(msg, level, showTime, null);
  28. }
  29. public void Init(string msg, TipLevel level, float showTime, Action finishCallBack)
  30. {
  31. switch (level)
  32. {
  33. case TipLevel.Normal:
  34. normal.gameObject.SetActive(true);
  35. warning.gameObject.SetActive(false);
  36. error.gameObject.SetActive(false);
  37. break;
  38. case TipLevel.Warning:
  39. normal.gameObject.SetActive(false);
  40. warning.gameObject.SetActive(true);
  41. error.gameObject.SetActive(false);
  42. break;
  43. case TipLevel.Error:
  44. normal.gameObject.SetActive(false);
  45. warning.gameObject.SetActive(false);
  46. error.gameObject.SetActive(true);
  47. break;
  48. }
  49. tipText.text = msg;
  50. this.showTime = showTime;
  51. elapsedTime = 0;
  52. this.finishCallBack = finishCallBack;
  53. }
  54. private void Update()
  55. {
  56. elapsedTime += Time.deltaTime;
  57. if (elapsedTime > showTime)
  58. {
  59. finishCallBack?.Invoke();
  60. Destroy(this.gameObject);
  61. }
  62. }
  63. }
  64. }