StoryDialogView.cs 43 KB

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