StoryDialogView.cs 38 KB

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