StoryDialogView.cs 40 KB

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