StoryDialogView.cs 39 KB

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