StoryDialogView.cs 43 KB

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