StoryDialogView.cs 37 KB

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