StoryDialogView.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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. //修改需求,所有关卡打开跳过(第一关除外)
  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. if(emojiValue != "0")
  762. {
  763. SceneController.UpdateDialogPic("0", _sceneObject);
  764. }
  765. UpdateLiveEmoji(emojiValue);
  766. }
  767. }
  768. private void UpdateLiveEmoji(string EmojiRes)
  769. {
  770. if (EmojiRes.Length > 0 && !IsTeaParty)
  771. {
  772. SceneController.UpdateEmojiAni(EmojiRes, ref _animObject);
  773. }
  774. }
  775. private void UpdateAnim(string value)
  776. {
  777. if (value.Length > 0 && !IsTeaParty)
  778. {
  779. if (value == "0")
  780. {
  781. //SceneController.ControlBgVisible(_sceneObject, true);
  782. _ui.m_hideAnimMask.Play();
  783. SceneController.UpdateDialogAnim(value, ref _animObject);
  784. }
  785. // 播放动画
  786. else
  787. {
  788. SceneController.UpdateDialogAnim(value, ref _animObject);
  789. _animObject.SetActive(false);
  790. _canClickBtnNext = false;
  791. _ui.m_showMask.Play(() =>
  792. {
  793. //_canClickBtnNext = true;
  794. if (_animObject != null)
  795. {
  796. _ui.m_hideMask.Play();
  797. _ui.m_showAnimMask.Play();
  798. _animObject.SetActive(true);
  799. }
  800. //SceneController.ControlBgVisible(_sceneObject, false);
  801. });
  802. }
  803. }
  804. }
  805. private void UpdateRoleObj(string value)
  806. {
  807. if (IsTeaParty)
  808. {
  809. var roleContainerList = LeagueDataManager.Instance.RoleContainerList;
  810. MyDressUpHelper.dressUpObj.setSceneObj(_sceneObject, false, false);
  811. for (int i = 0; i < roleContainerList.Count; i++)
  812. {
  813. if (value == (i + 1).ToString() && roleContainerList[i].EquipIds.Count > 0)
  814. {
  815. MyDressUpHelper.dressUpObj.PutOnItemList(roleContainerList[i].EquipIds);
  816. return;
  817. }
  818. }
  819. MyDressUpHelper.dressUpObj.PutOnDefaultDressUpData(IsTeaParty);
  820. }
  821. }
  822. private void UpdateMusic(string value)
  823. {
  824. if (value.Length > 0)
  825. {
  826. if (value == "0")
  827. {
  828. MusicManager.Instance.Stop();
  829. }
  830. else
  831. {
  832. MusicManager.Instance.PlayCroutine(ResPathUtil.GetMusicPath(value, "mp3"), false, 0.1f);
  833. }
  834. }
  835. }
  836. private void PlayEffect(string[] value)
  837. {
  838. if (value.Length > 0)
  839. {
  840. if (value[0] == "0")
  841. {
  842. EffectUIPool.Recycle(_effect);
  843. _effect = null;
  844. }
  845. else
  846. {
  847. EffectUIPool.Recycle(_effect);
  848. _effect = null;
  849. string effectPath = string.Format("ui_Juqing/{0}", value[0]);
  850. _effect = EffectUIPool.CreateEffectUI(_ui.m_effect, effectPath, value[0]);
  851. _ui.m_effect.visible = true;
  852. }
  853. }
  854. }
  855. private void PlayShake(int[] shakeInfoArr)
  856. {
  857. if (shakeInfoArr != null && shakeInfoArr.Length > 0)
  858. {
  859. Vector3 position = _sceneObject.transform.position;
  860. position.x = (float)shakeInfoArr[0] / GameConst.PIXELS_PER_UNITY_UNIT;
  861. position.y = (float)shakeInfoArr[1] / GameConst.PIXELS_PER_UNITY_UNIT;
  862. _sceneObject.transform.position = position;
  863. float attenuationX = (float)shakeInfoArr[2] / GameConst.PIXELS_PER_UNITY_UNIT;
  864. float attenuationY = (float)shakeInfoArr[3] / GameConst.PIXELS_PER_UNITY_UNIT;
  865. float interval = (float)shakeInfoArr[4] / 1000;
  866. float duration = (float)shakeInfoArr[5] / 1000;
  867. int repeat = Mathf.RoundToInt(duration / interval);
  868. Timers.inst.Add(interval, 0, UpdateShake, new float[] { attenuationX, attenuationY });
  869. }
  870. }
  871. private void UpdateShake(object param)
  872. {
  873. float[] attenuations = param as float[];
  874. float attenuationX = attenuations[0];
  875. float attenuationY = attenuations[1];
  876. Vector3 position = _sceneObject.transform.position;
  877. bool done = false;
  878. bool doneX = false;
  879. float absX = Mathf.Abs(position.x);
  880. if (absX > attenuationX)
  881. {
  882. int dir = (int)(position.x / absX);
  883. position.x = Mathf.Abs(position.x) - attenuationX;
  884. position.x *= -1 * dir;
  885. }
  886. else
  887. {
  888. doneX = true;
  889. position.x = 0;
  890. }
  891. bool doneY = false;
  892. float absY = Mathf.Abs(position.y);
  893. if (absY > attenuationY)
  894. {
  895. int dir = (int)(position.y / absY);
  896. position.y = Mathf.Abs(position.y) - attenuationY;
  897. position.y *= -1 * dir;
  898. }
  899. else
  900. {
  901. doneY = true;
  902. position.y = 0;
  903. }
  904. done = doneX && doneY;
  905. _sceneObject.transform.position = position;
  906. if (done)
  907. {
  908. Timers.inst.Remove(UpdateShake);
  909. }
  910. }
  911. private void Over(bool done = true)
  912. {
  913. if (_onCompleteStoryDialogCall != null)
  914. {
  915. _onCompleteStoryDialogCall(done, _onCompleteStoryDialogCallParam);
  916. }
  917. this.Hide();
  918. }
  919. private void UpdateSpeedUpBtn()
  920. {
  921. if (_speedAutoPlay > 1)
  922. {
  923. _ui.m_btnSpeedUp.text = "x" + _speedAutoPlay;
  924. }
  925. else
  926. {
  927. _ui.m_btnSpeedUp.text = "";
  928. }
  929. _typingEffect?.SetSpeed(_speedAutoPlay);
  930. }
  931. private void StopAutoPlay()
  932. {
  933. _autoPlay = false;
  934. _ui.m_btnAutoPlay.selected = false;
  935. Timers.inst.Remove(ShowNextWords);
  936. }
  937. private IEnumerator WaitCGAnimFinish(string resName, int times = 1)
  938. {
  939. GameObject cg = PrefabManager.Instance.InstantiateSync("test");
  940. Animator animator = cg.GetComponentInChildren<Animator>();
  941. AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0);
  942. yield return new WaitForSeconds(info.length * times);
  943. //while (info.normalizedTime < 0.95)
  944. //{
  945. // info = animator.GetCurrentAnimatorStateInfo(0);
  946. // yield return new WaitForSeconds(0.1f);
  947. //}
  948. PrefabManager.Instance.Restore(cg);
  949. OnStepComplete();
  950. }
  951. /// <summary>
  952. /// 检查资源加载是否完成
  953. /// </summary>
  954. private IEnumerator CheckResLoad()
  955. {
  956. List<string> resList = new List<string>();
  957. for (int i = 0; i < _stepListToRead.Count; i++)
  958. {
  959. StoryDialogCfg cfg = _stepListToRead[i];
  960. if (cfg.bgRes.Length > 0 && cfg.bgRes != "0")
  961. {
  962. string bgRes = ResPathUtil.GetSceneBgPath(cfg.bgRes);
  963. if (!resList.Contains(bgRes))
  964. {
  965. resList.Add(bgRes);
  966. }
  967. }
  968. if (cfg.aniRes.Length > 0 && cfg.aniRes != "0")
  969. {
  970. string res = cfg.aniRes.Split('/')[1];
  971. string aniRes = ResPathUtil.GetStoryDialogCGPath(cfg.aniRes, res);
  972. if (!resList.Contains(aniRes))
  973. {
  974. resList.Add(aniRes);
  975. }
  976. }
  977. if (cfg.emojiRes.Length > 0 && cfg.emojiRes != "0")
  978. {
  979. string EmojiPath = string.Format("DressUp/{0}", cfg.emojiRes);
  980. string aniRes = ResPathUtil.GetEmojiResPath(EmojiPath, cfg.emojiRes);
  981. if (!resList.Contains(aniRes))
  982. {
  983. resList.Add(aniRes);
  984. }
  985. }
  986. }
  987. ResourceDownloaderOperation downloaderOperation = YooAssets.CreateBundleDownloader(resList.ToArray(), 3, 3);
  988. if (downloaderOperation.TotalDownloadCount == 0)
  989. {
  990. ShowNextStep(_storyStartID);
  991. yield break;
  992. }
  993. // 开始加载资源
  994. ViewManager.Show<LoadingView>();
  995. LoadingView.Instance.SetDesc("正在加载剧情资源...");
  996. downloaderOperation.OnDownloadErrorCallback =
  997. (fileName, error) =>
  998. {
  999. Debug.LogError($"加载{fileName}失败 {error}");
  1000. };
  1001. downloaderOperation.OnDownloadProgressCallback =
  1002. (totalDownloadCount, currentDownloadCount, totalDownloadSizeBytes, currentDownloadSizeBytes) =>
  1003. {
  1004. string currentSizeMB = (currentDownloadSizeBytes / 1048576f).ToString("f1");
  1005. string totalSizeMB = (totalDownloadSizeBytes / 1048576f).ToString("f1");
  1006. var progress = (float)currentDownloadSizeBytes / totalDownloadSizeBytes;
  1007. //LauncherView.Instance.SetDesc($"正在下载资源,{currentDownloadCount}/{totalDownloadCount}", $"{currentSizeMB}MB/{totalSizeMB}MB", true);
  1008. LoadingView.Instance.SetProgress((int)(progress * 100));
  1009. };
  1010. downloaderOperation.BeginDownload();
  1011. yield return downloaderOperation;
  1012. // 检测下载结果
  1013. if (downloaderOperation.Status != EOperationStatus.Succeed)
  1014. {
  1015. Alert.Show("下载失败!请检查网络状态后重试。")
  1016. .SetLeftButton(true, "返回", (data) =>
  1017. {
  1018. ViewManager.Hide<LoadingView>();
  1019. Hide();
  1020. });
  1021. yield break;
  1022. }
  1023. // 加载完成
  1024. LoadingView.Instance.SetProgress(100, () =>
  1025. {
  1026. ViewManager.Hide<LoadingView>();
  1027. ShowNextStep(_storyStartID);
  1028. }
  1029. );
  1030. }
  1031. private RenderTexture renderTexure;
  1032. private void CreateHeadImg()
  1033. {
  1034. // 创建render texture
  1035. renderTexure = new RenderTexture(550, 760, 24);
  1036. // 换装父节点 + 相机
  1037. _dressUpSceneObj = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath("StoryDialogSelfImg/StoryDialogDressUpObj"));
  1038. Camera camera = _dressUpSceneObj.transform.Find("Camera").GetComponent<Camera>();
  1039. camera.targetTexture = renderTexure;
  1040. // 创建RawImg
  1041. _selfHeadImgObj = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath("StoryDialogSelfImg/Canvas"));
  1042. RawImage rawImage = _selfHeadImgObj.transform.Find("mask/RawImage").GetComponent<RawImage>();
  1043. rawImage.texture = renderTexure;
  1044. // 将RawImg放在FGUI上
  1045. GoWrapper goWrapper = new GoWrapper(_selfHeadImgObj);
  1046. _ui.m_dialogHead.m_compDressUp.m_holder.SetNativeObject(goWrapper);
  1047. }
  1048. private void DestroyHeadImg()
  1049. {
  1050. // 清空render Texture
  1051. Camera camera = _dressUpSceneObj.transform.Find("Camera").GetComponent<Camera>();
  1052. camera.targetTexture = null;
  1053. RawImage rawImage = _selfHeadImgObj.transform.Find("mask/RawImage").GetComponent<RawImage>();
  1054. rawImage.texture = null;
  1055. renderTexure.Release();
  1056. renderTexure = null;
  1057. // 回收人物
  1058. _dressUpObj?.Dispose();
  1059. _dressUpObj = null;
  1060. // 回收RawImage
  1061. if (_selfHeadImgObj != null)
  1062. {
  1063. PrefabManager.Instance.Restore(_selfHeadImgObj);
  1064. _selfHeadImgObj = null;
  1065. }
  1066. // 回收相机预制
  1067. if (_dressUpSceneObj != null)
  1068. {
  1069. PrefabManager.Instance.Restore(_dressUpSceneObj);
  1070. _dressUpSceneObj = null;
  1071. }
  1072. }
  1073. /// <summary>
  1074. /// 修改主角头像组件层级
  1075. /// </summary>
  1076. private void ChangeHeadImgLayer()
  1077. {
  1078. if(_dressUpSceneObj == null)
  1079. {
  1080. return;
  1081. }
  1082. Transform parent = _dressUpSceneObj.transform.Find("Role");
  1083. // HeadImg的层级:6
  1084. ChangeLayer(parent, 6);
  1085. GRoot.inst.touchable = true;
  1086. }
  1087. private void ChangeLayer(Transform transform, int layer)
  1088. {
  1089. if(transform == null)
  1090. {
  1091. return;
  1092. }
  1093. if (transform.childCount > 0)
  1094. {
  1095. for (int i = 0; i < transform.childCount; i++)
  1096. {
  1097. ChangeLayer(transform.GetChild(i), layer);
  1098. }
  1099. transform.gameObject.layer = layer;
  1100. }
  1101. else
  1102. {
  1103. transform.gameObject.layer = layer;
  1104. }
  1105. }
  1106. }
  1107. }