StoryDialogView.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 RenderTexture renderTexure;
  903. private void CreateHeadImg()
  904. {
  905. // 创建render texture
  906. renderTexure = new RenderTexture(550, 760, 24);
  907. // 换装父节点 + 相机
  908. _dressUpSceneObj = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath("StoryDialogSelfImg/StoryDialogDressUpObj"));
  909. Camera camera = _dressUpSceneObj.transform.Find("Camera").GetComponent<Camera>();
  910. camera.targetTexture = renderTexure;
  911. // 创建RawImg
  912. _selfHeadImgObj = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath("StoryDialogSelfImg/Canvas"));
  913. RawImage rawImage = _selfHeadImgObj.transform.Find("mask/RawImage").GetComponent<RawImage>();
  914. rawImage.texture = renderTexure;
  915. // 将RawImg放在FGUI上
  916. GoWrapper goWrapper = new GoWrapper(_selfHeadImgObj);
  917. _ui.m_dialogHead.m_compDressUp.m_holder.SetNativeObject(goWrapper);
  918. // FGUI会自动修改Canvas的参数,需要调整一下位置
  919. RectTransform canvas = _selfHeadImgObj.transform.GetComponent<RectTransform>();
  920. canvas.pivot = Vector2.one * 0.5f;
  921. canvas.anchoredPosition = Vector2.zero;
  922. }
  923. private void DestroyHeadImg()
  924. {
  925. // 清空render Texture
  926. Camera camera = _dressUpSceneObj.transform.Find("Camera").GetComponent<Camera>();
  927. camera.targetTexture = null;
  928. RawImage rawImage = _selfHeadImgObj.transform.Find("mask/RawImage").GetComponent<RawImage>();
  929. rawImage.texture = null;
  930. renderTexure.Release();
  931. renderTexure = null;
  932. // 回收人物
  933. _dressUpObj?.Dispose();
  934. _dressUpObj = null;
  935. // 回收RawImage
  936. if (_selfHeadImgObj != null)
  937. {
  938. PrefabManager.Instance.Restore(_selfHeadImgObj);
  939. _selfHeadImgObj = null;
  940. }
  941. // 回收相机预制
  942. if (_dressUpSceneObj != null)
  943. {
  944. PrefabManager.Instance.Restore(_dressUpSceneObj);
  945. _dressUpSceneObj = null;
  946. }
  947. }
  948. /// <summary>
  949. /// 修改主角头像组件层级
  950. /// </summary>
  951. private void ChangeHeadImgLayer()
  952. {
  953. Transform parent = _dressUpSceneObj.transform.Find("Role");
  954. ChangeLayer(parent, parent.gameObject.layer);
  955. }
  956. private void ChangeLayer(Transform transform, int layer)
  957. {
  958. if (transform.childCount > 0)
  959. {
  960. for (int i = 0; i < transform.childCount; i++)
  961. {
  962. ChangeLayer(transform.GetChild(i), layer);
  963. }
  964. transform.gameObject.layer = layer;
  965. }
  966. else
  967. {
  968. transform.gameObject.layer = layer;
  969. }
  970. }
  971. }
  972. }