StoryDialogView.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. using FairyGUI;
  2. using UnityEngine;
  3. using UI.Main;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text.RegularExpressions;
  7. using System;
  8. using YooAsset;
  9. using System.Threading.Tasks;
  10. using GFGGame.Launcher;
  11. namespace GFGGame
  12. {
  13. public delegate void OnCompleteStoryDialogCall(bool isSkip, object param);
  14. public class StoryDialogView : BaseView
  15. {
  16. private UI_StoryDialogUI _ui;
  17. private UI_CompArrow _arrow;
  18. private GameObject _sceneObject;
  19. private GameObject _animObject;
  20. private EffectUI _effectUI1;
  21. private DressUpObjUI _dressUpObjUI;
  22. private GTextField _wordTextField;
  23. //剧情完成回调
  24. private OnCompleteStoryDialogCall _onCompleteStoryDialogCall;
  25. private object _onCompleteStoryDialogCallParam;
  26. //回顾
  27. private List<string> _dialogListLookBack;
  28. //自动播放
  29. private int _speedAutoPlay = 1;
  30. private bool _autoPlay = false;
  31. //剧情状态
  32. private List<StoryDialogCfg> _stepListToRead;
  33. private StoryDialogCfg _currentStepCfg;
  34. private string _nextStepId;
  35. private string[] _wordList;
  36. private int _wordIndex = 0;
  37. private bool _isShowLetters;
  38. private bool _canClickBtnNext;
  39. private string _currentWords;
  40. private string _storyStartID;
  41. private string lastTextFieldType; // 上一段文本框的类型
  42. private bool IsTeaParty; //是否是茶话会里的对话
  43. TypingFadeEffectPro _typingEffect;
  44. //屏幕效果中
  45. private Action<object> _onScreenEffectComplete;
  46. public override void Dispose()
  47. {
  48. if (_sceneObject != null)
  49. {
  50. PrefabManager.Instance.Restore(_sceneObject);
  51. _sceneObject = null;
  52. }
  53. if(_animObject != null)
  54. {
  55. PrefabManager.Instance.Restore(_animObject);
  56. _animObject = null;
  57. }
  58. _dressUpObjUI?.Dispose();
  59. _dressUpObjUI = null;
  60. _wordTextField = null;
  61. _arrow = null;
  62. _isShowLetters = false;
  63. EffectUIPool.Recycle(_effectUI1);
  64. _effectUI1 = null;
  65. _onScreenEffectComplete = null;
  66. _typingEffect = null;
  67. if (_ui != null)
  68. {
  69. _ui.Dispose();
  70. _ui = null;
  71. }
  72. base.Dispose();
  73. }
  74. protected override void OnInit()
  75. {
  76. base.OnInit();
  77. packageName = UI_StoryDialogUI.PACKAGE_NAME;
  78. _ui = UI_StoryDialogUI.Create();
  79. viewCom = _ui.target;
  80. isfullScreen = true;
  81. isReturnView = true;
  82. _ui.m_dialogText.target.visible = false;
  83. _ui.m_dialogName.target.visible = false;
  84. _ui.m_dialogHead.target.visible = false;
  85. _ui.m_list.visible = false;
  86. _ui.m_btnNext.width = GRoot.inst.width;
  87. _ui.m_btnNext.height = GRoot.inst.height;
  88. _ui.m_mask1.height = (GRoot.inst.height - 1920) / 2;
  89. _ui.m_mask2.height = (GRoot.inst.height - 1920) / 2;
  90. _ui.m_mask1.y = 0;
  91. _ui.m_mask2.y = GRoot.inst.height - _ui.m_mask2.height;
  92. _ui.m_btnNext.AddRelation(GRoot.inst, RelationType.Size);
  93. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  94. _ui.m_btnNext.onClick.Add(OnClickBtnNext);
  95. _ui.m_btnLookBack.onClick.Add(OnClickBtnLookBack);
  96. _ui.m_btnSkip.onClick.Add(OnBtnSkip);
  97. _ui.m_list.onClickItem.Add(OnClickListItem);
  98. _ui.m_btnSpeedUp.onClick.Add(OnClickBtnSpeedUp);
  99. _ui.m_btnAutoPlay.onClick.Add(OnClickBtnAutoPlay);
  100. }
  101. protected override void OnShown()
  102. {
  103. base.OnShown();
  104. // 初始化
  105. MusicManager.Instance.Stop();
  106. MusicManager.Instance.SetNormalVolumn(0.6f);
  107. MusicManager.Instance.SetVolume(0);
  108. _ui.m_hideAnimMask.Play();
  109. if (_sceneObject == null)
  110. {
  111. _sceneObject = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath("SceneStoryDialog"));
  112. }
  113. if (_dressUpObjUI == null)
  114. {
  115. _dressUpObjUI = new DressUpObjUI();
  116. }
  117. _speedAutoPlay = FightDataManager.Instance.dialogSpeed;
  118. lastTextFieldType = "";
  119. UpdateSpeedUpBtn();
  120. _dialogListLookBack = new List<string>();
  121. object[] datas = viewData as object[];
  122. _storyStartID = (string)datas[0];
  123. bool skipable = (bool)datas[1];
  124. _onCompleteStoryDialogCall = (OnCompleteStoryDialogCall)datas[2];
  125. if (datas.Length > 3)
  126. {
  127. _onCompleteStoryDialogCallParam = datas[3];
  128. }
  129. IsTeaParty = (bool)datas[4];
  130. _autoPlay = IsTeaParty;
  131. if (LauncherConfig.netType == LauncherConfig.EnumNetType.TEMP && !InstanceZonesDataManager.CheckLevelPass(MainStoryDataManager.currentLevelCfgId))
  132. {
  133. // 临时设置都可以跳过对话
  134. skipable = false;
  135. }
  136. else
  137. {
  138. skipable = true;
  139. }
  140. _ui.m_btnSkip.visible = skipable;
  141. _ui.m_c1.selectedIndex = 0;
  142. _ui.m_btnAutoPlay.selected = false;
  143. if (_storyStartID == MainStoryDataManager.priorId)
  144. {
  145. _ui.m_c1.selectedIndex = 1;
  146. OnClickBtnAutoPlay();
  147. _speedAutoPlay = 1;
  148. FightDataManager.Instance.dialogSpeed = _speedAutoPlay;
  149. }
  150. if (IsTeaParty)
  151. {
  152. _ui.m_c1.selectedIndex = 2;
  153. _speedAutoPlay = 1;
  154. }
  155. _ui.m_btnBack.visible = InstanceZonesDataManager.CheckLevelPass(100001001);
  156. StoryDialogDataManager.Instance.Clear();
  157. InitStepListById(_storyStartID);
  158. // 检查资源的初始化
  159. Timers.inst.StartCoroutine(CheckResLoad());
  160. }
  161. protected override void OnHide()
  162. {
  163. base.OnHide();
  164. Timers.inst.Remove(UpdateShake);
  165. Timers.inst.Remove(OnScreenEffectComplete);
  166. Timers.inst.Remove(ShowNextWords);
  167. ScreenBlackController.Instance.HideBlack();
  168. StopAutoPlay();
  169. VoiceManager.Instance.StopVoice();
  170. if (_sceneObject != null)
  171. {
  172. PrefabManager.Instance.Restore(_sceneObject);
  173. _sceneObject = null;
  174. }
  175. if (_animObject != null)
  176. {
  177. PrefabManager.Instance.Restore(_animObject);
  178. _animObject = null;
  179. }
  180. _dressUpObjUI.dressUpObj.TakeOffAll();
  181. MusicManager.Instance.PlayCroutine(ResPathUtil.GetMusicPath(ConstMusicName.DEFAULT));
  182. _onCompleteStoryDialogCall = null;
  183. _onCompleteStoryDialogCallParam = null;
  184. StoryDialogDataManager.Instance.Clear();
  185. GuideController.TryCompleteGuide(ConstGuideId.ENTER_CHAPTER_5, 3);
  186. MusicManager.Instance.SetNormalVolumn(1);
  187. }
  188. protected override void TryCompleteGuide()
  189. {
  190. base.TryCompleteGuide();
  191. GuideController.TryCompleteGuide(ConstGuideId.ENTER_CHAPTER_5, 3);
  192. }
  193. private void OnClickBtnBack()
  194. {
  195. Over(false);
  196. }
  197. private void OnClickBtnNext()
  198. {
  199. if (!_canClickBtnNext || IsTeaParty)
  200. {
  201. return;
  202. }
  203. StopAutoPlay();
  204. if (_onScreenEffectComplete != null)
  205. {
  206. Timers.inst.Remove(OnScreenEffectComplete);
  207. _onScreenEffectComplete.Invoke(null);
  208. }
  209. else
  210. {
  211. ShowNextWords();
  212. }
  213. }
  214. private void OnClickBtnLookBack()
  215. {
  216. StopAutoPlay();
  217. if (_ui.m_btnSkip.visible)
  218. {
  219. ViewManager.Show<StoryLookBackView>(_storyStartID);
  220. }
  221. else
  222. {
  223. ViewManager.Show<StoryLookBackView>(_dialogListLookBack);
  224. }
  225. }
  226. private void OnBtnSkip()
  227. {
  228. Over(true);
  229. }
  230. private void OnClickListItem(EventContext context)
  231. {
  232. UI_ListDialogItem dialogItem = UI_ListDialogItem.Proxy(context.data as GObject);
  233. _dialogListLookBack.Add(dialogItem.m_txtContent.text);
  234. string stepID = (string)dialogItem.target.data;
  235. if (stepID == null)
  236. {
  237. stepID = "0";
  238. }
  239. UI_ListDialogItem.ProxyEnd();
  240. OnStepComplete(stepID);
  241. }
  242. private void OnClickBtnSpeedUp()
  243. {
  244. //如果没有自动播放先开始自动播放
  245. if (!_autoPlay)
  246. {
  247. _ui.m_btnAutoPlay.selected = true;
  248. OnClickBtnAutoPlay();
  249. }
  250. _speedAutoPlay = _speedAutoPlay * 2;
  251. if (_speedAutoPlay > GameConst.MAX_SPEED_AUTO_PLAY)
  252. {
  253. _speedAutoPlay = 1;
  254. }
  255. FightDataManager.Instance.dialogSpeed = _speedAutoPlay;
  256. UpdateSpeedUpBtn();
  257. }
  258. private void OnClickBtnAutoPlay()
  259. {
  260. _autoPlay = _ui.m_btnAutoPlay.selected;
  261. if (_autoPlay)
  262. {
  263. ShowNextWords();
  264. }
  265. }
  266. private void InitStepListById(string dialogID)
  267. {
  268. var temp = StoryDialogCfgArray.Instance.GetCfgsByid(dialogID);
  269. _stepListToRead = new List<StoryDialogCfg>(temp);
  270. }
  271. private void ShowNextStep(string nextStepId)
  272. {
  273. if (nextStepId != null)
  274. {
  275. InitStepListById(nextStepId);
  276. }
  277. if (_stepListToRead != null && _stepListToRead.Count > 0)
  278. {
  279. StoryDialogCfg storyDialogCfg = (StoryDialogCfg)_stepListToRead[0];
  280. _stepListToRead.RemoveAt(0);
  281. Timers.inst.StartCoroutine(InitStepContent(storyDialogCfg));
  282. }
  283. else
  284. {
  285. Over();
  286. }
  287. }
  288. private void OnStepComplete(string nextStepId = null)
  289. {
  290. _nextStepId = nextStepId;
  291. _ui.m_dialogText.target.visible = false;
  292. _ui.m_dialogName.target.visible = false;
  293. _ui.m_dialogHead.target.visible = false;
  294. float delay = 0;
  295. //屏幕效果
  296. if (_currentStepCfg != null)
  297. {
  298. if (_currentStepCfg.blackScreenDur > 0)
  299. {
  300. delay = _currentStepCfg.blackScreenDur;
  301. ScreenBlackController.Instance.ShowBlack(delay, this.viewCom, 0);
  302. }
  303. else if (_currentStepCfg.blankScreenDur > 0)
  304. {
  305. delay = _currentStepCfg.blankScreenDur;
  306. UpdatePic("0");
  307. UpdateAnim("0");
  308. }
  309. }
  310. if (delay > 0)
  311. {
  312. //转换成秒
  313. delay = delay / 1000f;
  314. _onScreenEffectComplete = OnScreenEffectComplete;
  315. Timers.inst.Add(delay, 1, OnScreenEffectComplete);
  316. StoryDialogDataManager.Instance.dialogShowDelay = 0.6f;
  317. }
  318. else
  319. {
  320. OnScreenEffectComplete();
  321. }
  322. }
  323. private void OnScreenEffectComplete(object param = null)
  324. {
  325. _onScreenEffectComplete = null;
  326. if (_nextStepId == "0")
  327. {
  328. Over();
  329. }
  330. else
  331. {
  332. ShowNextStep(_nextStepId);
  333. }
  334. }
  335. private IEnumerator InitStepContent(StoryDialogCfg storyDialogCfg)
  336. {
  337. _canClickBtnNext = false;
  338. StoryDialogDataManager.Instance.waitPicFade = false;
  339. _ui.m_hide.Play(); // 隐藏遮罩
  340. HideAllDialogUI();
  341. // Init resource
  342. _currentStepCfg = storyDialogCfg;
  343. UpdateMusic(storyDialogCfg.musicRes);
  344. UpdateBg(storyDialogCfg.bgRes);
  345. UpdatePic(storyDialogCfg.picRes);
  346. UpdateAnim(storyDialogCfg.aniRes);
  347. UpdateRoleObj(storyDialogCfg.name);
  348. PlayEffect(storyDialogCfg.effectInfoArr);
  349. PlayShake(storyDialogCfg.shakeInfoArr);
  350. string content = storyDialogCfg.content;
  351. content = storyDialogCfg.content.Replace("self", RoleDataManager.roleName);
  352. while (StoryDialogDataManager.Instance.waitBgChange)
  353. {
  354. //Debug.Log("等待背景转换");
  355. yield return new WaitForEndOfFrame();
  356. }
  357. if(StoryDialogDataManager.Instance.dialogShowDelay > 0)
  358. {
  359. yield return new WaitForSeconds(StoryDialogDataManager.Instance.dialogShowDelay);
  360. StoryDialogDataManager.Instance.dialogShowDelay = 0f;
  361. }
  362. _canClickBtnNext = true;
  363. if (content.IndexOf("//") >= 0)
  364. {
  365. showList(content);
  366. }
  367. else
  368. {
  369. Timers.inst.StartCoroutine(ShowDialog(storyDialogCfg));
  370. }
  371. }
  372. private void showList(string content)
  373. {
  374. StopAutoPlay();
  375. _ui.m_btnAutoPlay.enabled = false;
  376. _wordTextField = null;
  377. _ui.m_list.visible = true;
  378. _ui.m_list.RemoveChildrenToPool();
  379. string[] list = Regex.Split(content, "//");
  380. _ui.m_list.itemRenderer = (int index, GObject item) =>
  381. {
  382. string itemInfo = list[index];
  383. string[] itemInfoList = Regex.Split(itemInfo, "=");
  384. UI_ListDialogItem dialogItem = UI_ListDialogItem.Proxy(item);
  385. dialogItem.m_txtContent.text = itemInfoList[0];
  386. dialogItem.target.data = itemInfoList.Length > 1 ? itemInfoList[1] : null;
  387. UI_ListDialogItem.ProxyEnd();
  388. };
  389. _ui.m_list.numItems = list.Length;
  390. }
  391. private void HideAllDialogUI()
  392. {
  393. _ui.m_dialogText.target.visible = false;
  394. _ui.m_dialogName.target.visible = false;
  395. _ui.m_dialogHead.target.visible = false;
  396. _ui.m_list.visible = false;
  397. }
  398. /// <summary>
  399. /// 初始化对话框/语音/CG等
  400. /// </summary>
  401. /// <param name="storyDialogCfg"></param>
  402. /// <returns></returns>
  403. private IEnumerator ShowDialog(StoryDialogCfg storyDialogCfg)
  404. {
  405. if (storyDialogCfg.showChangeName == 1 && StorageDataManager.Instance.GetStorageValue(ConstStorageId.CHANGE_NAME) == 0)
  406. {
  407. GameController.ShowCreateRole();
  408. StoryDialogDataManager.Instance.waiting = true;
  409. while (StoryDialogDataManager.Instance.waiting)
  410. {
  411. yield return new WaitForEndOfFrame();
  412. }
  413. if (_autoPlay)
  414. {
  415. _ui.m_btnAutoPlay.selected = false;
  416. OnClickBtnAutoPlay();
  417. }
  418. }
  419. _ui.m_loaMask.url = ResPathUtil.GetBgImgPath(storyDialogCfg.maskRes);
  420. _ui.m_btnAutoPlay.enabled = true;
  421. _ui.m_list.visible = false;
  422. var content = storyDialogCfg.content.Replace("self", RoleDataManager.roleName);
  423. string words = content;
  424. string roleName = storyDialogCfg.name;
  425. string headRes = storyDialogCfg.head;
  426. string headAniRes = storyDialogCfg.headAni;
  427. string[] effectInfo = storyDialogCfg.effectInfoArr;
  428. VoiceManager.Instance.StopVoice();
  429. // 如果配置了语音,读取语音
  430. VoiceManager.Instance.LoadRes(ResPathUtil.GetVoicePath(storyDialogCfg.voiceRes));
  431. _wordList = Regex.Split(words, "&&");
  432. // 有对话
  433. if (_wordList.Length > 0 && !_wordList[0].Equals(""))
  434. {
  435. if (roleName == "self")
  436. {
  437. roleName = RoleDataManager.roleName;
  438. }
  439. //回顾
  440. if (roleName != null)
  441. {
  442. _dialogListLookBack.Add("[color=#FDA2B1]" + roleName + "[/color]");
  443. }
  444. if (!string.IsNullOrEmpty(headAniRes) || storyDialogCfg.suitId > 0)
  445. {
  446. //显示对话框半身像
  447. _ui.m_dialogHead.target.visible = true;
  448. _ui.m_dialogHead.m_txtName.text = roleName;
  449. _ui.m_dialogHead.m_comphead.m_head.visible = false;
  450. _ui.m_dialogHead.m_comphead.m_holder.visible = true;
  451. var headAniCfg = HeadAniCfgArray.Instance.GetCfg(headAniRes);
  452. if (headAniCfg != null && !string.IsNullOrEmpty(headAniCfg.headAni))
  453. {
  454. //独立动画
  455. _ui.m_dialogHead.m_compDressUp.target.visible = false;
  456. _ui.m_dialogHead.m_comphead.target.visible = true;
  457. _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_dialogHead.m_comphead.m_holder, "ui_nzbq", headAniCfg.headAni);
  458. }
  459. else
  460. {
  461. //换装
  462. _ui.m_dialogHead.m_compDressUp.target.visible = true;
  463. _ui.m_dialogHead.m_comphead.target.visible = false;
  464. _dressUpObjUI.ResetSceneObj(80, true, false, null, false);
  465. if (storyDialogCfg.suitId > 0)
  466. {
  467. _dressUpObjUI.dressUpObj.PutOnSuitCfg(storyDialogCfg.suitId, false, new int[] { ConstDressUpItemType.SHOU_CHI_WU }, false, false);
  468. }
  469. else
  470. {
  471. _dressUpObjUI.dressUpObj.PutOnDressUpData(CustomSuitDataManager.GetCurrentSuitData().dressUpData, new int[] { ConstDressUpItemType.SHOU_CHI_WU });
  472. if (_dressUpObjUI.dressUpObj.actionId > 0)
  473. {
  474. _dressUpObjUI.dressUpObj.CancelAction(true, new int[] { ConstDressUpItemType.SHOU_CHI_WU });
  475. }
  476. }
  477. if (headAniCfg != null && headAniCfg.faceId > 0)
  478. {
  479. //表情
  480. _dressUpObjUI.dressUpObj.AddOrRemove(headAniCfg.faceId, true);
  481. _dressUpObjUI.UpdateWrapper(_ui.m_dialogHead.m_compDressUp.m_holder);
  482. }
  483. }
  484. _wordTextField = _ui.m_dialogHead.m_txtContent;
  485. _arrow = _ui.m_dialogHead.m_iconNext;
  486. lastTextFieldType = "head";
  487. }
  488. else if (!string.IsNullOrEmpty(headRes))
  489. {
  490. _ui.m_dialogHead.target.visible = true;
  491. _ui.m_dialogHead.m_txtName.text = roleName;
  492. _ui.m_dialogHead.m_comphead.m_head.visible = true;
  493. _ui.m_dialogHead.m_comphead.m_holder.visible = false;
  494. _ui.m_dialogHead.m_comphead.m_head.url = ResPathUtil.GetNpcHeadPath(headRes);
  495. _wordTextField = _ui.m_dialogHead.m_txtContent;
  496. _arrow = _ui.m_dialogHead.m_iconNext;
  497. lastTextFieldType = "head";
  498. }
  499. else if (!string.IsNullOrEmpty(roleName))
  500. {
  501. _ui.m_dialogName.target.visible = true;
  502. if (IsTeaParty)
  503. {
  504. var roleContainerList = LeagueDataManager.Instance.RoleContainerList;
  505. var teapartyRoleCfg = TeapartyRoleCfgArray.Instance.GetCfgsByid(LeagueDataManager.Instance.TeaPartyId);
  506. int roleIndex = Convert.ToInt32(roleName);
  507. _ui.m_dialogName.m_txtName.text = teapartyRoleCfg[roleIndex - 1].name;
  508. _ui.m_dialogName.m_bgType.selectedIndex = 1;
  509. _ui.m_dialogName.m_showArrow.selectedIndex = 1;
  510. if (roleIndex <= roleContainerList.Count && roleContainerList[roleIndex - 1].MaxScoreRoleName != null && roleContainerList[roleIndex - 1].MaxScoreRoleName != "" && _ui.m_dialogName.m_txtName.text != "")
  511. {
  512. _ui.m_comPlayName.visible = true;
  513. _ui.m_txtPlayName.text = roleContainerList[roleIndex - 1].MaxScoreRoleName;
  514. if (_ui.m_txtPlayName.text == RoleDataManager.roleName)
  515. _ui.m_dialogName.m_bgType.selectedIndex = 0;
  516. }
  517. else
  518. _ui.m_comPlayName.visible = false;
  519. }
  520. else
  521. {
  522. _ui.m_dialogName.m_bgType.selectedIndex = 0;
  523. _ui.m_dialogName.m_showArrow.selectedIndex = 0;
  524. _ui.m_dialogName.m_txtName.text = roleName;
  525. }
  526. _wordTextField = _ui.m_dialogName.m_txtContent;
  527. _arrow = _ui.m_dialogName.m_iconNext;
  528. if (!lastTextFieldType.Equals("name"))
  529. {
  530. lastTextFieldType = "name";
  531. StoryDialogDataManager.Instance.waiting = true;
  532. _ui.m_t0.Play(() =>
  533. {
  534. StoryDialogDataManager.Instance.waiting = false;
  535. });
  536. }
  537. }
  538. else
  539. {
  540. if (IsTeaParty)
  541. _ui.m_dialogText.m_showArrow.selectedIndex = 1;
  542. else
  543. _ui.m_dialogText.m_showArrow.selectedIndex = 0;
  544. _ui.m_dialogText.target.visible = true;
  545. _wordTextField = _ui.m_dialogText.m_txtContent;
  546. _arrow = _ui.m_dialogText.m_iconNext;
  547. lastTextFieldType = "text";
  548. _ui.m_comPlayName.visible = false;
  549. }
  550. _wordIndex = 0;
  551. _typingEffect = new TypingFadeEffectPro(_wordTextField);
  552. _typingEffect.typeFinishedAction = ShowCurrentWords;
  553. ShowNextDialog();
  554. }
  555. else
  556. {
  557. OnStepComplete();
  558. //if (effectInfo.Length > 0)
  559. //{
  560. // // 等待CG播放完毕直接进入下一段
  561. // Timers.inst.StartCoroutine(WaitCGAnimFinish(effectInfo[0], int.Parse(effectInfo[1])));
  562. //}
  563. //// 播放效果等
  564. //else
  565. //{
  566. // OnStepComplete();
  567. //}
  568. }
  569. }
  570. private void ShowNextDialog()
  571. {
  572. if (_wordList != null && _wordList.Length > _wordIndex)
  573. {
  574. string itemInfo = _wordList[_wordIndex];
  575. string[] itemInfoList = Regex.Split(itemInfo, "=");
  576. _currentWords = itemInfoList[0];
  577. if (itemInfoList.Length > 1)
  578. {
  579. _wordTextField.data = itemInfoList[1];
  580. }
  581. else
  582. {
  583. _wordTextField.data = null;
  584. }
  585. Timers.inst.StartCoroutine(StartShowLetters());
  586. }
  587. else
  588. {
  589. OnStepComplete();
  590. }
  591. }
  592. private void ShowCurrentWords()
  593. {
  594. //_arrow.target.visible = true;
  595. //Timers.inst.Remove(UpdateLetters);
  596. StopTyping();
  597. _typingEffect?.Cancel();
  598. _wordTextField.text = _currentWords;
  599. _dialogListLookBack.Add(_currentWords);
  600. _isShowLetters = false;
  601. _wordIndex++;
  602. if (_autoPlay)
  603. {
  604. int interval = (int)Mathf.Max(VoiceManager.Instance.GetClipLength(), GameConst.NEXT_WORDS_INTERVAL_MAX / _speedAutoPlay);
  605. Timers.inst.Add(interval, 1, ShowNextWords);
  606. }
  607. }
  608. private void ShowNextWords(object param = null)
  609. {
  610. if (_wordTextField != null)
  611. {
  612. if (_isShowLetters)
  613. {
  614. ShowCurrentWords();
  615. }
  616. else
  617. {
  618. string stepID = (string)_wordTextField.data;
  619. if (stepID != null)
  620. {
  621. OnStepComplete(stepID);
  622. }
  623. else
  624. {
  625. ShowNextDialog();
  626. }
  627. }
  628. }
  629. }
  630. private IEnumerator StartShowLetters()
  631. {
  632. _isShowLetters = true;
  633. _arrow.target.visible = false;
  634. _wordTextField.verticalAlign = VertAlignType.Top;
  635. _wordTextField.text = "";
  636. _canClickBtnNext = false;
  637. while (StoryDialogDataManager.Instance.waitPicFade || StoryDialogDataManager.Instance.waiting)
  638. {
  639. //Debug.Log("等待立绘/动画结束");
  640. yield return new WaitForEndOfFrame();
  641. }
  642. _canClickBtnNext = true;
  643. _wordTextField.text = _currentWords;
  644. StartTyping();
  645. }
  646. /// <summary>
  647. /// 开启打字机显示
  648. /// </summary>
  649. private void StartTyping()
  650. {
  651. _typingEffect.SetSpeed(_speedAutoPlay);
  652. _typingEffect.Start();
  653. // 如果配置了语音,则播放语音
  654. VoiceManager.Instance.PlayVoice();
  655. }
  656. private void StopTyping()
  657. {
  658. _typingEffect.Cancel();
  659. }
  660. private void UpdateBg(string value)
  661. {
  662. if (value.Length > 0)
  663. {
  664. SceneController.UpdateDialogBg(value, _sceneObject);
  665. }
  666. }
  667. private void UpdatePic(string value)
  668. {
  669. if(value.Length > 0 && !IsTeaParty)
  670. {
  671. SceneController.UpdateDialogPic(value, _sceneObject);
  672. }
  673. }
  674. private void UpdateAnim(string value)
  675. {
  676. if (value.Length > 0 && !IsTeaParty)
  677. {
  678. if (value == "0")
  679. {
  680. //SceneController.ControlBgVisible(_sceneObject, true);
  681. _ui.m_hideAnimMask.Play();
  682. SceneController.UpdateDialogAnim(value, ref _animObject);
  683. }
  684. // 播放动画
  685. else
  686. {
  687. StoryDialogDataManager.Instance.waiting = true;
  688. _ui.m_showMask.Play(() =>
  689. {
  690. _ui.m_hideMask.Play();
  691. _ui.m_showAnimMask.Play();
  692. //SceneController.ControlBgVisible(_sceneObject, false);
  693. SceneController.UpdateDialogAnim(value, ref _animObject);
  694. });
  695. }
  696. }
  697. }
  698. private void UpdateRoleObj(string value)
  699. {
  700. if (IsTeaParty)
  701. {
  702. var roleContainerList = LeagueDataManager.Instance.RoleContainerList;
  703. MyDressUpHelper.dressUpObj.setSceneObj(_sceneObject, false, false);
  704. for (int i = 0; i < roleContainerList.Count; i++)
  705. {
  706. if (value == (i + 1).ToString() && roleContainerList[i].EquipIds.Count > 0)
  707. {
  708. MyDressUpHelper.dressUpObj.PutOnItemList(roleContainerList[i].EquipIds);
  709. return;
  710. }
  711. }
  712. MyDressUpHelper.dressUpObj.PutOnDefaultDressUpData(IsTeaParty);
  713. }
  714. }
  715. private void UpdateMusic(string value)
  716. {
  717. if (value.Length > 0)
  718. {
  719. if (value == "0")
  720. {
  721. MusicManager.Instance.Stop();
  722. }
  723. else
  724. {
  725. MusicManager.Instance.PlayCroutine(ResPathUtil.GetMusicPath(value, "mp3"), false, 0.1f);
  726. }
  727. }
  728. }
  729. private void PlayEffect(string[] infos)
  730. {
  731. }
  732. private void PlayShake(int[] shakeInfoArr)
  733. {
  734. if (shakeInfoArr != null && shakeInfoArr.Length > 0)
  735. {
  736. Vector3 position = _sceneObject.transform.position;
  737. position.x = (float)shakeInfoArr[0] / GameConst.PIXELS_PER_UNITY_UNIT;
  738. position.y = (float)shakeInfoArr[1] / GameConst.PIXELS_PER_UNITY_UNIT;
  739. _sceneObject.transform.position = position;
  740. float attenuationX = (float)shakeInfoArr[2] / GameConst.PIXELS_PER_UNITY_UNIT;
  741. float attenuationY = (float)shakeInfoArr[3] / GameConst.PIXELS_PER_UNITY_UNIT;
  742. float interval = (float)shakeInfoArr[4] / 1000;
  743. float duration = (float)shakeInfoArr[5] / 1000;
  744. int repeat = Mathf.RoundToInt(duration / interval);
  745. Timers.inst.Add(interval, 0, UpdateShake, new float[] { attenuationX, attenuationY });
  746. }
  747. }
  748. private void UpdateShake(object param)
  749. {
  750. float[] attenuations = param as float[];
  751. float attenuationX = attenuations[0];
  752. float attenuationY = attenuations[1];
  753. Vector3 position = _sceneObject.transform.position;
  754. bool done = false;
  755. bool doneX = false;
  756. float absX = Mathf.Abs(position.x);
  757. if (absX > attenuationX)
  758. {
  759. int dir = (int)(position.x / absX);
  760. position.x = Mathf.Abs(position.x) - attenuationX;
  761. position.x *= -1 * dir;
  762. }
  763. else
  764. {
  765. doneX = true;
  766. position.x = 0;
  767. }
  768. bool doneY = false;
  769. float absY = Mathf.Abs(position.y);
  770. if (absY > attenuationY)
  771. {
  772. int dir = (int)(position.y / absY);
  773. position.y = Mathf.Abs(position.y) - attenuationY;
  774. position.y *= -1 * dir;
  775. }
  776. else
  777. {
  778. doneY = true;
  779. position.y = 0;
  780. }
  781. done = doneX && doneY;
  782. _sceneObject.transform.position = position;
  783. if (done)
  784. {
  785. Timers.inst.Remove(UpdateShake);
  786. }
  787. }
  788. private void Over(bool isSkip = true)
  789. {
  790. if (_onCompleteStoryDialogCall != null)
  791. {
  792. _onCompleteStoryDialogCall(isSkip, _onCompleteStoryDialogCallParam);
  793. }
  794. }
  795. private void UpdateSpeedUpBtn()
  796. {
  797. if (_speedAutoPlay > 1)
  798. {
  799. _ui.m_btnSpeedUp.text = "x" + _speedAutoPlay;
  800. }
  801. else
  802. {
  803. _ui.m_btnSpeedUp.text = "";
  804. }
  805. _typingEffect?.SetSpeed(_speedAutoPlay);
  806. }
  807. private void StopAutoPlay()
  808. {
  809. _autoPlay = false;
  810. _ui.m_btnAutoPlay.selected = false;
  811. Timers.inst.Remove(ShowNextWords);
  812. }
  813. private IEnumerator WaitCGAnimFinish(string resName, int times = 1)
  814. {
  815. GameObject cg = PrefabManager.Instance.InstantiateSync("test");
  816. Animator animator = cg.GetComponentInChildren<Animator>();
  817. AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0);
  818. yield return new WaitForSeconds(info.length * times);
  819. //while (info.normalizedTime < 0.95)
  820. //{
  821. // info = animator.GetCurrentAnimatorStateInfo(0);
  822. // yield return new WaitForSeconds(0.1f);
  823. //}
  824. PrefabManager.Instance.Restore(cg);
  825. OnStepComplete();
  826. }
  827. /// <summary>
  828. /// 检查资源加载是否完成
  829. /// </summary>
  830. private IEnumerator CheckResLoad()
  831. {
  832. List<string> resList = new List<string>();
  833. for (int i = 0; i < _stepListToRead.Count; i++)
  834. {
  835. StoryDialogCfg cfg = _stepListToRead[i];
  836. if (cfg.bgRes.Length > 0)
  837. {
  838. string bgRes = ResPathUtil.GetSceneBgPath(cfg.bgRes);
  839. if (!resList.Contains(bgRes))
  840. {
  841. resList.Add(bgRes);
  842. }
  843. }
  844. if (cfg.aniRes.Length > 0)
  845. {
  846. string res = cfg.aniRes.Split('/')[1];
  847. string aniRes = ResPathUtil.GetStoryDialogCGPath(cfg.aniRes, res);
  848. if (!resList.Contains(aniRes))
  849. {
  850. resList.Add(aniRes);
  851. }
  852. }
  853. }
  854. ResourceDownloaderOperation downloaderOperation = YooAssets.CreateBundleDownloader(resList.ToArray(), 3, 3);
  855. if (downloaderOperation.TotalDownloadCount == 0)
  856. {
  857. ShowNextStep(_storyStartID);
  858. yield break;
  859. }
  860. // 开始加载资源
  861. ViewManager.Show<LoadingView>();
  862. LoadingView.Instance.SetDesc("正在加载剧情资源...");
  863. downloaderOperation.OnDownloadErrorCallback =
  864. (fileName, error) =>
  865. {
  866. Debug.LogError($"加载{fileName}失败 {error}");
  867. };
  868. downloaderOperation.OnDownloadProgressCallback =
  869. (totalDownloadCount, currentDownloadCount, totalDownloadSizeBytes, currentDownloadSizeBytes) =>
  870. {
  871. string currentSizeMB = (currentDownloadSizeBytes / 1048576f).ToString("f1");
  872. string totalSizeMB = (totalDownloadSizeBytes / 1048576f).ToString("f1");
  873. var progress = (float)currentDownloadSizeBytes / totalDownloadSizeBytes;
  874. //LauncherView.Instance.SetDesc($"正在下载资源,{currentDownloadCount}/{totalDownloadCount}", $"{currentSizeMB}MB/{totalSizeMB}MB", true);
  875. LoadingView.Instance.SetProgress((int)(progress * 100));
  876. };
  877. downloaderOperation.BeginDownload();
  878. yield return downloaderOperation;
  879. // 检测下载结果
  880. if (downloaderOperation.Status != EOperationStatus.Succeed)
  881. {
  882. Alert.Show("下载失败!请检查网络状态后重试。")
  883. .SetLeftButton(true, "返回", (data) =>
  884. {
  885. ViewManager.Hide<LoadingView>();
  886. Hide();
  887. });
  888. yield break;
  889. }
  890. ViewManager.Hide<LoadingView>();
  891. ShowNextStep(_storyStartID);
  892. }
  893. }
  894. }