StoryDialogView.cs 39 KB

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