StoryDialogView.cs 38 KB

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