StoryDialogView.cs 42 KB

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