DialoguePanel.cs 4.2 KB

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