DialoguePanel.cs 7.7 KB

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