DialoguePanel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Text;
  2. using Common.Utility.CombatEvent;
  3. using UnityEngine;
  4. namespace Fort23.Mono
  5. {
  6. [UIBinding(prefab = "DialoguePanel")]
  7. public partial class DialoguePanel : UIPanel
  8. {
  9. private string[] dialogueMessaga;
  10. private ShowDialogueEventData.MessageShowType messageShowType;
  11. private System.Action finish;
  12. private int index;
  13. private char[] _currShowMessage;
  14. private float _showTime = 0.05f;
  15. private float _currShowTime = 0;
  16. protected StringBuilder _sb = new StringBuilder();
  17. private int _currShowIndex;
  18. private bool _isUpdate;
  19. private bool _isShowNextButton;
  20. public static async void OpenDialoguePanel(string[] dialogueMessaga,
  21. ShowDialogueEventData.MessageShowType messageShowType,
  22. System.Action finish)
  23. {
  24. DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialoguePanel>(null, UILayer.Top);
  25. // dialoguePanel.GObjectPoolInterface.SetActive(false);
  26. dialoguePanel.ShowPanel(dialogueMessaga, messageShowType, finish);
  27. }
  28. private void Init()
  29. {
  30. }
  31. protected override void AddEvent()
  32. {
  33. }
  34. protected override void DelEvent()
  35. {
  36. }
  37. public override void AddButtonEvent()
  38. {
  39. nextButton.onClick.AddListener(NextShow);
  40. }
  41. private void NextShow()
  42. {
  43. StartShowMassge();
  44. }
  45. public void ShowPanel(string[] dialogueMessaga, ShowDialogueEventData.MessageShowType messageShowType,
  46. System.Action finish)
  47. {
  48. this.dialogueMessaga = dialogueMessaga;
  49. this.messageShowType = messageShowType;
  50. this.finish = finish;
  51. index = 0;
  52. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  53. StartShowMassge();
  54. }
  55. private void StartShowMassge()
  56. {
  57. if (index >= dialogueMessaga.Length)
  58. {
  59. UIManager.Instance.HideUIUIPanel(this);
  60. finish?.Invoke();
  61. return;
  62. }
  63. _isShowNextButton = false;
  64. nextIcon.SetActive(false);
  65. string m = dialogueMessaga[index];
  66. index++;
  67. switch (messageShowType)
  68. {
  69. case ShowDialogueEventData.MessageShowType.Default:
  70. message.text = m;
  71. ShowNextIcon();
  72. break;
  73. case ShowDialogueEventData.MessageShowType.Verbatim:
  74. _sb.Clear();
  75. _currShowMessage = m.ToCharArray();
  76. _sb.Append(_currShowMessage[0]);
  77. message.text = _sb.ToString();
  78. _isUpdate = true;
  79. _currShowIndex = 1;
  80. break;
  81. }
  82. }
  83. private void ShowNextIcon()
  84. {
  85. nextIcon.SetActive(true);
  86. _isShowNextButton = true;
  87. }
  88. public override void Hide()
  89. {
  90. base.Hide();
  91. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  92. }
  93. public void Update()
  94. {
  95. if (!_isUpdate)
  96. {
  97. return;
  98. }
  99. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  100. {
  101. if (_currShowIndex >= _currShowMessage.Length)
  102. {
  103. _isUpdate = false;
  104. ShowNextIcon();
  105. return;
  106. }
  107. _currShowTime += Time.deltaTime;
  108. if (_currShowTime > _showTime)
  109. {
  110. _currShowTime -= _showTime;
  111. _sb.Append(_currShowMessage[_currShowIndex]);
  112. _currShowIndex++;
  113. message.text = _sb.ToString();
  114. }
  115. }
  116. }
  117. }
  118. }