DialoguePanel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. private string[] showIconName;
  21. public static async void OpenDialoguePanel(string[] dialogueMessaga, string[] icon,
  22. ShowDialogueEventData.MessageShowType messageShowType,
  23. System.Action finish)
  24. {
  25. DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialoguePanel>(null, UILayer.Top);
  26. // dialoguePanel.GObjectPoolInterface.SetActive(false);
  27. dialoguePanel.ShowPanel(dialogueMessaga, icon, messageShowType, finish);
  28. }
  29. private void Init()
  30. {
  31. }
  32. protected override void AddEvent()
  33. {
  34. }
  35. protected override void DelEvent()
  36. {
  37. }
  38. public override void AddButtonEvent()
  39. {
  40. nextButton.onClick.AddListener(NextShow);
  41. }
  42. private void NextShow()
  43. {
  44. StartShowMassge();
  45. }
  46. public void ShowPanel(string[] dialogueMessaga, string[] icon,
  47. ShowDialogueEventData.MessageShowType messageShowType,
  48. System.Action finish)
  49. {
  50. showIconName = icon;
  51. this.dialogueMessaga = dialogueMessaga;
  52. this.messageShowType = messageShowType;
  53. this.finish = finish;
  54. index = 0;
  55. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  56. StartShowMassge();
  57. }
  58. private void StartShowMassge()
  59. {
  60. if (index >= dialogueMessaga.Length)
  61. {
  62. UIManager.Instance.HideUIUIPanel(this);
  63. finish?.Invoke();
  64. return;
  65. }
  66. _isShowNextButton = false;
  67. nextIcon.SetActive(false);
  68. string m = dialogueMessaga[index];
  69. if (showIconName != null && index < showIconName.Length)
  70. {
  71. icon.icon_name= showIconName[index];
  72. }
  73. index++;
  74. switch (messageShowType)
  75. {
  76. case ShowDialogueEventData.MessageShowType.Default:
  77. message.text = m;
  78. ShowNextIcon();
  79. break;
  80. case ShowDialogueEventData.MessageShowType.Verbatim:
  81. _sb.Clear();
  82. _currShowMessage = m.ToCharArray();
  83. _sb.Append(_currShowMessage[0]);
  84. message.text = _sb.ToString();
  85. _isUpdate = true;
  86. _currShowIndex = 1;
  87. break;
  88. }
  89. }
  90. private void ShowNextIcon()
  91. {
  92. nextIcon.SetActive(true);
  93. _isShowNextButton = true;
  94. }
  95. public override void Hide()
  96. {
  97. base.Hide();
  98. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  99. }
  100. public void Update()
  101. {
  102. if (!_isUpdate)
  103. {
  104. return;
  105. }
  106. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  107. {
  108. if (_currShowIndex >= _currShowMessage.Length)
  109. {
  110. _isUpdate = false;
  111. ShowNextIcon();
  112. return;
  113. }
  114. _currShowTime += Time.deltaTime;
  115. if (_currShowTime > _showTime)
  116. {
  117. _currShowTime -= _showTime;
  118. _sb.Append(_currShowMessage[_currShowIndex]);
  119. _currShowIndex++;
  120. message.text = _sb.ToString();
  121. }
  122. }
  123. }
  124. }
  125. }