UISubtitleController.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Text;
  3. using DG.Tweening;
  4. using ExtensionMethods;
  5. using UnityEngine;
  6. public class UISubtitleController : MonoBehaviour
  7. {
  8. private bool IsShow
  9. {
  10. get
  11. {
  12. return this.label.alpha > 0f;
  13. }
  14. }
  15. private void Awake()
  16. {
  17. R.Ui.UISubtitle = this;
  18. }
  19. private void Start()
  20. {
  21. this.label.alpha = 0f;
  22. }
  23. public void Show(string subtitle)
  24. {
  25. subtitle = UISubtitleController.Pretreatment(subtitle);
  26. this.label.text = subtitle;
  27. this.label.alpha = 1f;
  28. }
  29. public void Hide()
  30. {
  31. this.label.alpha = 0f;
  32. }
  33. public YieldInstruction FadeIn(string subtitle)
  34. {
  35. if (this._fadeIn != null && this._fadeIn.IsPlaying())
  36. {
  37. this._fadeIn.Kill(false);
  38. this._fadeIn = null;
  39. }
  40. if (this._fadeOut != null && this._fadeOut.IsPlaying())
  41. {
  42. this._fadeOut.Kill(false);
  43. this._fadeOut = null;
  44. }
  45. subtitle = UISubtitleController.Pretreatment(subtitle);
  46. this.label.text = subtitle;
  47. this._fadeIn = this.label.DOFade(0.8f, 1.5f);
  48. this._fadeIn.OnComplete(delegate
  49. {
  50. this._fadeIn = null;
  51. });
  52. return this._fadeIn.WaitForCompletion();
  53. }
  54. public YieldInstruction FadeOut()
  55. {
  56. if (this._fadeIn != null)
  57. {
  58. this._fadeIn.Kill(false);
  59. this._fadeIn = null;
  60. }
  61. if (this._fadeOut != null || !this.IsShow)
  62. {
  63. return null;
  64. }
  65. this._fadeOut = this.label.DOFade(0f, 1.5f);
  66. this._fadeOut.OnComplete(delegate
  67. {
  68. this._fadeOut = null;
  69. });
  70. return this._fadeOut.WaitForCompletion();
  71. }
  72. private static string Pretreatment(string str)
  73. {
  74. return new StringBuilder(str).Insert(0, "[-]").Replace("[-]", "[FFFFFF]").Replace("\\n", "\n").ToString();
  75. }
  76. [SerializeField]
  77. private UILabel label;
  78. private Tweener _fadeIn;
  79. private Tweener _fadeOut;
  80. }