StoryDialogView.cs 43 KB

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