StoryDialogView.cs 40 KB

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