StoryDialogView.cs 43 KB

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