StoryDialogView.cs 39 KB

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