StoryDialogView.cs 35 KB

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