DialoguePanel.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System.Text;
  2. using Common.Utility.CombatEvent;
  3. using Core.Language;
  4. using Excel2Json;
  5. using Fort23.UTool;
  6. using UnityEngine;
  7. using System;
  8. using Fort23.Core;
  9. namespace Fort23.Mono
  10. {
  11. [UIBinding(prefab = "DialoguePanel")]
  12. public partial class DialoguePanel : UIPanel
  13. {
  14. private int[] dialogueMessaga;
  15. private ShowDialogueEventData.MessageShowType messageShowType;
  16. private Action<int?> finish;
  17. private int index;
  18. private char[] _currShowMessage;
  19. private float _showTime = 0.05f;
  20. private float _currShowTime = 0;
  21. protected StringBuilder _sb = new StringBuilder();
  22. private int _currShowIndex;
  23. private bool _isUpdate;
  24. private bool _isShowNextButton;
  25. private string[] showIconName;
  26. private EventConditionConfig eventConditionConfig;
  27. private bool _skipTyping;
  28. private bool _isShowingOptions;
  29. public static async void OpenDialoguePanel(int id, string[] icon,
  30. ShowDialogueEventData.MessageShowType messageShowType,
  31. Action<int?> finish)
  32. {
  33. DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialoguePanel>(null, UILayer.Top);
  34. dialoguePanel.ShowPanel(id, icon, messageShowType, finish);
  35. }
  36. private void Init()
  37. {
  38. _skipTyping = false;
  39. _isShowingOptions = false;
  40. }
  41. protected override void AddEvent()
  42. {
  43. }
  44. protected override void DelEvent()
  45. {
  46. }
  47. public override void AddButtonEvent()
  48. {
  49. nextButton.onClick.AddListener(NextShow);
  50. }
  51. private void SkipTyping()
  52. {
  53. _skipTyping = true;
  54. if (_isUpdate)
  55. {
  56. _sb.Clear();
  57. _sb.Append(_currShowMessage);
  58. message.text = _sb.ToString();
  59. _currShowIndex = _currShowMessage.Length;
  60. _isUpdate = false;
  61. ShowNextIcon();
  62. }
  63. }
  64. private void NextShow()
  65. {
  66. if(_isShowingOptions)
  67. return;
  68. if (_isUpdate)
  69. {
  70. SkipTyping();
  71. }
  72. else
  73. {
  74. StartShowMassge();
  75. }
  76. }
  77. public void ShowPanel(int id, string[] icon,
  78. ShowDialogueEventData.MessageShowType messageShowType,
  79. Action<int?> finish)
  80. {
  81. eventConditionConfig = ConfigComponent.Instance.Get<EventConditionConfig>(id);
  82. showIconName = icon;
  83. this.dialogueMessaga = eventConditionConfig.LanID;
  84. this.messageShowType = messageShowType;
  85. this.finish = finish;
  86. index = 0;
  87. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  88. StartShowMassge();
  89. }
  90. private void StartShowMassge()
  91. {
  92. if (index >= dialogueMessaga.Length)
  93. {
  94. // 所有句子显示完成,检查是否有选项
  95. if (eventConditionConfig.optionType == 1 && !_isShowingOptions)
  96. {
  97. ShowOptions();
  98. }
  99. else
  100. {
  101. // 无选项,关闭面板,返回 null
  102. UIManager.Instance.HideUIUIPanel(this);
  103. finish?.Invoke(null);
  104. }
  105. return;
  106. }
  107. _isShowNextButton = false;
  108. nextIcon.SetActive(false);
  109. string m = LanguageManager.Instance.Text(dialogueMessaga[index]);
  110. if (showIconName != null && index < showIconName.Length)
  111. {
  112. icon.icon_name = showIconName[index];
  113. }
  114. index++;
  115. switch (messageShowType)
  116. {
  117. case ShowDialogueEventData.MessageShowType.Default:
  118. message.text = m;
  119. ShowNextIcon();
  120. break;
  121. case ShowDialogueEventData.MessageShowType.Verbatim:
  122. _sb.Clear();
  123. _currShowMessage = m.ToCharArray();
  124. _sb.Append(_currShowMessage[0]);
  125. message.text = _sb.ToString();
  126. _isUpdate = true;
  127. _currShowIndex = 1;
  128. _skipTyping = false;
  129. break;
  130. }
  131. }
  132. private async void ShowOptions()
  133. {
  134. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  135. _isShowingOptions = true;
  136. foreach (var op in eventConditionConfig.optionPara1)
  137. {
  138. // EventConditionConfig eventConditionConfig = ConfigComponent.Instance.Get<EventConditionConfig>(op);
  139. DialogueOptionWidget dialogueOptionWidget =
  140. await UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null,OptionRoot);
  141. dialogueOptionWidget.CustomInit(op,SelectOption);
  142. }
  143. }
  144. private void SelectOption(DialogueOptionWidget obj)
  145. {
  146. int selectedOptionID = obj.eventConditionConfig.ID;
  147. UIManager.Instance.HideUIUIPanel(this);
  148. finish?.Invoke(selectedOptionID);
  149. }
  150. private void ShowNextIcon()
  151. {
  152. nextIcon.SetActive(true);
  153. _isShowNextButton = true;
  154. }
  155. public override void Hide()
  156. {
  157. base.Hide();
  158. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  159. }
  160. public void Update()
  161. {
  162. if (!_isUpdate) return;
  163. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  164. {
  165. if (_currShowIndex >= _currShowMessage.Length)
  166. {
  167. _isUpdate = false;
  168. ShowNextIcon();
  169. return;
  170. }
  171. _currShowTime += Time.deltaTime;
  172. if (_currShowTime > _showTime)
  173. {
  174. _currShowTime -= _showTime;
  175. _sb.Append(_currShowMessage[_currShowIndex]);
  176. _currShowIndex++;
  177. message.text = _sb.ToString();
  178. }
  179. }
  180. }
  181. public override void Close()
  182. {
  183. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  184. _isShowingOptions = false;
  185. _skipTyping = false;
  186. base.Close();
  187. }
  188. }
  189. }