UIToast.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections;
  3. using DG.Tweening;
  4. using UnityEngine;
  5. public class UIToast : MonoBehaviour
  6. {
  7. public UILabel Label
  8. {
  9. get
  10. {
  11. return this.content;
  12. }
  13. }
  14. public void Appear(string text, int fontSize, bool withBackground = true)
  15. {
  16. this._background.SetActive(withBackground);
  17. this.content.text = text;
  18. this.content.fontSize = fontSize;
  19. this.content.text = text;
  20. this.content.fontSize = fontSize;
  21. this.toastPanel.alpha = 1f;
  22. this.toastPanel.gameObject.SetActive(true);
  23. }
  24. public void Disappear()
  25. {
  26. this.toastPanel.gameObject.SetActive(false);
  27. }
  28. public Coroutine Show(string text, float duration = 2f, bool withBackground = true)
  29. {
  30. return this.Show(text, 48, duration, withBackground);
  31. }
  32. public Coroutine Show(string text, int fontSize, float duration, bool withBackground = true)
  33. {
  34. return base.StartCoroutine(this.ShowCoroutine(text, fontSize, duration, withBackground));
  35. }
  36. private IEnumerator ShowCoroutine(string text, int fontSize, float duration, bool withBackground)
  37. {
  38. this._background.SetActive(withBackground);
  39. this.content.text = text;
  40. this.content.fontSize = fontSize;
  41. this.content.text = text;
  42. this.content.fontSize = fontSize;
  43. this.toastPanel.gameObject.SetActive(true);
  44. yield return DOTween.To(() => this.toastPanel.alpha, delegate(float a)
  45. {
  46. this.toastPanel.alpha = a;
  47. }, 1f, 0.5f).WaitForCompletion();
  48. yield return new WaitForSeconds((duration < 1f) ? 0f : (duration - 1f));
  49. yield return DOTween.To(() => this.toastPanel.alpha, delegate(float a)
  50. {
  51. this.toastPanel.alpha = a;
  52. }, 0f, 0.5f).WaitForCompletion();
  53. this.toastPanel.gameObject.SetActive(false);
  54. yield break;
  55. }
  56. [SerializeField]
  57. private UIPanel toastPanel;
  58. [SerializeField]
  59. private UILabel content;
  60. [SerializeField]
  61. private GameObject _background;
  62. }