DialoguePanel.cs 7.8 KB

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