StoryDialogView.cs 40 KB

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