StoryDialogView.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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.netType != LauncherConfig.EnumNetType.LOCAL && !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. 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. //屏幕效果
  316. if (_currentStepCfg != null)
  317. {
  318. if (_currentStepCfg.blackScreenDur > 0)
  319. {
  320. delay = _currentStepCfg.blackScreenDur;
  321. ScreenBlackController.Instance.ShowBlack(delay, this.viewCom, 0);
  322. }
  323. else if (_currentStepCfg.blankScreenDur > 0)
  324. {
  325. delay = _currentStepCfg.blankScreenDur;
  326. UpdatePic("0");
  327. UpdateAnim("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);
  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. if (storyDialogCfg.showChangeName == 1 && StorageDataManager.Instance.GetStorageValue(ConstStorageId.CHANGE_NAME) == 0)
  434. {
  435. GameController.ShowCreateRole();
  436. StoryDialogDataManager.Instance.waiting = true;
  437. while (StoryDialogDataManager.Instance.waiting)
  438. {
  439. yield return new WaitForEndOfFrame();
  440. }
  441. //if (_autoPlay)
  442. //{
  443. // _ui.m_btnAutoPlay.selected = false;
  444. // OnClickBtnAutoPlay();
  445. //}
  446. }
  447. _ui.m_loaMask.url = ResPathUtil.GetBgImgPath(storyDialogCfg.maskRes);
  448. _ui.m_btnAutoPlay.enabled = true;
  449. _ui.m_list.visible = false;
  450. var content = storyDialogCfg.content.Replace("self", RoleDataManager.roleName);
  451. string words = content;
  452. string roleName = storyDialogCfg.name;
  453. string headRes = storyDialogCfg.head;
  454. string headAniRes = storyDialogCfg.headAni;
  455. string[] effectInfo = storyDialogCfg.effectInfoArr;
  456. VoiceManager.Instance.StopVoice();
  457. // 如果没开倍速才加载语音
  458. if(_speedAutoPlay == 1 || !_autoPlay)
  459. {
  460. // 如果配置了语音,读取语音
  461. VoiceManager.Instance.LoadRes(ResPathUtil.GetVoicePath(storyDialogCfg.voiceRes));
  462. }
  463. _wordList = Regex.Split(words, "&&");
  464. // 有对话
  465. if (_wordList.Length > 0 && !_wordList[0].Equals(""))
  466. {
  467. if (roleName == "self")
  468. {
  469. roleName = RoleDataManager.roleName;
  470. }
  471. //回顾
  472. if (roleName != null)
  473. {
  474. _dialogListLookBack.Add("[color=#FDA2B1]" + roleName + "[/color]");
  475. }
  476. if (!string.IsNullOrEmpty(headAniRes) || storyDialogCfg.suitId > 0)
  477. {
  478. //显示对话框半身像
  479. _ui.m_dialogHead.m_txtName.text = roleName;
  480. _ui.m_dialogHead.m_comphead.m_head.visible = false;
  481. _ui.m_dialogHead.m_comphead.m_holder.visible = true;
  482. _ui.m_dialogHead.m_txtContent.text = "";
  483. var headAniCfg = HeadAniCfgArray.Instance.GetCfg(headAniRes);
  484. if (headAniCfg != null && !string.IsNullOrEmpty(headAniCfg.headAni))
  485. {
  486. //独立动画
  487. _ui.m_dialogHead.m_compDressUp.target.visible = false;
  488. _ui.m_dialogHead.m_comphead.target.visible = true;
  489. _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_dialogHead.m_comphead.m_holder, "ui_nzbq", headAniCfg.headAni);
  490. }
  491. else
  492. {
  493. //换装
  494. _ui.m_dialogHead.m_comphead.target.visible = false;
  495. //_dressUpObjUI.ResetSceneObj(80, true, false, sceneObject.transform.Find("Scene").gameObject, false);
  496. _dressUpObj.setSceneObj(_dressUpSceneObj, false, false, null, false, ChangeHeadImgLayer);
  497. if (storyDialogCfg.suitId > 0)
  498. {
  499. _dressUpObj.PutOnSuitCfg(storyDialogCfg.suitId, false, new int[] { ConstDressUpItemType.SHOU_CHI_WU }, false, false);
  500. }
  501. else
  502. {
  503. _dressUpObj.PutOnDressUpData(CustomSuitDataManager.GetCurrentSuitData().dressUpData, new int[] { ConstDressUpItemType.SHOU_CHI_WU });
  504. if (_dressUpObj.actionId > 0)
  505. {
  506. _dressUpObj.CancelAction(true, new int[] { ConstDressUpItemType.SHOU_CHI_WU });
  507. }
  508. }
  509. if (headAniCfg != null && headAniCfg.faceId > 0)
  510. {
  511. //表情
  512. _dressUpObj.AddOrRemove(headAniCfg.faceId, true);
  513. //_dressUpObjUI.UpdateWrapper(_ui.m_dialogHead.m_compDressUp.m_holder);
  514. }
  515. _ui.m_dialogHead.m_compDressUp.target.visible = true;
  516. }
  517. _ui.m_dialogHead.target.visible = true;
  518. _wordTextField = _ui.m_dialogHead.m_txtContent;
  519. _arrow = _ui.m_dialogHead.m_iconNext;
  520. lastTextFieldType = "head";
  521. }
  522. else if (!string.IsNullOrEmpty(headRes))
  523. {
  524. _ui.m_dialogHead.target.visible = true;
  525. _ui.m_dialogHead.m_txtName.text = roleName;
  526. _ui.m_dialogHead.m_comphead.m_head.visible = true;
  527. _ui.m_dialogHead.m_comphead.m_holder.visible = false;
  528. _ui.m_dialogHead.m_comphead.m_head.url = ResPathUtil.GetNpcHeadPath(headRes);
  529. _wordTextField = _ui.m_dialogHead.m_txtContent;
  530. _arrow = _ui.m_dialogHead.m_iconNext;
  531. lastTextFieldType = "head";
  532. }
  533. else if (!string.IsNullOrEmpty(roleName))
  534. {
  535. _ui.m_dialogName.target.visible = true;
  536. if (IsTeaParty)
  537. {
  538. var roleContainerList = LeagueDataManager.Instance.RoleContainerList;
  539. var teapartyRoleCfg = TeapartyRoleCfgArray.Instance.GetCfgsByid(LeagueDataManager.Instance.TeaPartyId);
  540. int roleIndex = Convert.ToInt32(roleName);
  541. _ui.m_dialogName.m_txtName.text = teapartyRoleCfg[roleIndex - 1].name;
  542. _ui.m_dialogName.m_bgType.selectedIndex = 1;
  543. _ui.m_dialogName.m_showArrow.selectedIndex = 1;
  544. if (roleIndex <= roleContainerList.Count && roleContainerList[roleIndex - 1].MaxScoreRoleName != null && roleContainerList[roleIndex - 1].MaxScoreRoleName != "" && _ui.m_dialogName.m_txtName.text != "")
  545. {
  546. _ui.m_comPlayName.visible = true;
  547. _ui.m_txtPlayName.text = roleContainerList[roleIndex - 1].MaxScoreRoleName;
  548. if (_ui.m_txtPlayName.text == RoleDataManager.roleName)
  549. _ui.m_dialogName.m_bgType.selectedIndex = 0;
  550. }
  551. else
  552. _ui.m_comPlayName.visible = false;
  553. }
  554. else
  555. {
  556. _ui.m_dialogName.m_bgType.selectedIndex = 0;
  557. _ui.m_dialogName.m_showArrow.selectedIndex = 0;
  558. _ui.m_dialogName.m_txtName.text = roleName;
  559. }
  560. _wordTextField = _ui.m_dialogName.m_txtContent;
  561. _arrow = _ui.m_dialogName.m_iconNext;
  562. if (!lastTextFieldType.Equals("name"))
  563. {
  564. lastTextFieldType = "name";
  565. StoryDialogDataManager.Instance.waiting = true;
  566. _ui.m_t0.Play(() =>
  567. {
  568. StoryDialogDataManager.Instance.waiting = false;
  569. });
  570. }
  571. }
  572. else
  573. {
  574. if (IsTeaParty)
  575. _ui.m_dialogText.m_showArrow.selectedIndex = 1;
  576. else
  577. _ui.m_dialogText.m_showArrow.selectedIndex = 0;
  578. _ui.m_dialogText.target.visible = true;
  579. _wordTextField = _ui.m_dialogText.m_txtContent;
  580. _arrow = _ui.m_dialogText.m_iconNext;
  581. lastTextFieldType = "text";
  582. _ui.m_comPlayName.visible = false;
  583. }
  584. _wordIndex = 0;
  585. _typingEffect = new TypingFadeEffectPro(_wordTextField);
  586. _typingEffect.typeFinishedAction = ShowCurrentWords;
  587. ShowNextDialog();
  588. }
  589. else
  590. {
  591. OnStepComplete();
  592. //if (effectInfo.Length > 0)
  593. //{
  594. // // 等待CG播放完毕直接进入下一段
  595. // Timers.inst.StartCoroutine(WaitCGAnimFinish(effectInfo[0], int.Parse(effectInfo[1])));
  596. //}
  597. //// 播放效果等
  598. //else
  599. //{
  600. // OnStepComplete();
  601. //}
  602. }
  603. }
  604. /// <summary>
  605. /// 延迟显示头像,防止换装异步加载/换表情 未完成时显示了头像
  606. /// </summary>
  607. private async void ShowSelfHeadImg()
  608. {
  609. _ui.m_dialogHead.m_compDressUp.target.visible = false;
  610. await Task.Delay(80);
  611. _ui.m_dialogHead.m_compDressUp.target.visible = true;
  612. }
  613. private void ShowNextDialog()
  614. {
  615. if (_wordList != null && _wordList.Length > _wordIndex)
  616. {
  617. string itemInfo = _wordList[_wordIndex];
  618. string[] itemInfoList = Regex.Split(itemInfo, "=");
  619. _currentWords = itemInfoList[0];
  620. if (itemInfoList.Length > 1)
  621. {
  622. _wordTextField.data = itemInfoList[1];
  623. }
  624. else
  625. {
  626. _wordTextField.data = null;
  627. }
  628. Timers.inst.StartCoroutine(StartShowLetters());
  629. }
  630. else
  631. {
  632. OnStepComplete();
  633. }
  634. }
  635. private void ShowCurrentWords()
  636. {
  637. //_arrow.target.visible = true;
  638. //Timers.inst.Remove(UpdateLetters);
  639. StopTyping();
  640. _typingEffect?.Cancel();
  641. _wordTextField.text = _currentWords;
  642. _dialogListLookBack.Add(_currentWords);
  643. _isShowLetters = false;
  644. _wordIndex++;
  645. if (_autoPlay)
  646. {
  647. float interval = Mathf.Max(VoiceManager.Instance.GetClipRemainingLength(), GameConst.NEXT_WORDS_INTERVAL_MAX / _speedAutoPlay);
  648. Timers.inst.Add(interval, 1, ShowNextWords);
  649. }
  650. }
  651. private void ShowNextWords(object param = null)
  652. {
  653. if (_wordTextField != null)
  654. {
  655. if (_isShowLetters)
  656. {
  657. ShowCurrentWords();
  658. }
  659. else
  660. {
  661. string stepID = (string)_wordTextField.data;
  662. if (stepID != null)
  663. {
  664. OnStepComplete(stepID);
  665. }
  666. else
  667. {
  668. ShowNextDialog();
  669. }
  670. }
  671. }
  672. }
  673. private IEnumerator StartShowLetters()
  674. {
  675. _isShowLetters = true;
  676. _arrow.target.visible = false;
  677. _wordTextField.verticalAlign = VertAlignType.Top;
  678. _wordTextField.text = "";
  679. _canClickBtnNext = false;
  680. while (StoryDialogDataManager.Instance.waitPicFade || StoryDialogDataManager.Instance.waiting)
  681. {
  682. //Debug.Log("等待立绘/动画结束");
  683. yield return new WaitForEndOfFrame();
  684. }
  685. _canClickBtnNext = true;
  686. _wordTextField.text = _currentWords;
  687. StartTyping();
  688. }
  689. /// <summary>
  690. /// 开启打字机显示
  691. /// </summary>
  692. private void StartTyping()
  693. {
  694. _typingEffect.SetSpeed(_speedAutoPlay);
  695. _typingEffect.Start();
  696. // 如果配置了语音,则播放语音
  697. VoiceManager.Instance.PlayVoice();
  698. }
  699. private void StopTyping()
  700. {
  701. _typingEffect.Cancel();
  702. }
  703. private void UpdateBg(string value)
  704. {
  705. if (value.Length > 0)
  706. {
  707. SceneController.UpdateDialogBg(value, _sceneObject);
  708. }
  709. }
  710. private void UpdatePic(string value)
  711. {
  712. if(value.Length > 0 && !IsTeaParty)
  713. {
  714. SceneController.UpdateDialogPic(value, _sceneObject);
  715. }
  716. }
  717. private void UpdateAnim(string value)
  718. {
  719. if (value.Length > 0 && !IsTeaParty)
  720. {
  721. if (value == "0")
  722. {
  723. //SceneController.ControlBgVisible(_sceneObject, true);
  724. _ui.m_hideAnimMask.Play();
  725. SceneController.UpdateDialogAnim(value, ref _animObject);
  726. }
  727. // 播放动画
  728. else
  729. {
  730. StoryDialogDataManager.Instance.waiting = true;
  731. _ui.m_showMask.Play(() =>
  732. {
  733. _ui.m_hideMask.Play();
  734. _ui.m_showAnimMask.Play();
  735. //SceneController.ControlBgVisible(_sceneObject, false);
  736. SceneController.UpdateDialogAnim(value, ref _animObject);
  737. });
  738. }
  739. }
  740. }
  741. private void UpdateRoleObj(string value)
  742. {
  743. if (IsTeaParty)
  744. {
  745. var roleContainerList = LeagueDataManager.Instance.RoleContainerList;
  746. MyDressUpHelper.dressUpObj.setSceneObj(_sceneObject, false, false);
  747. for (int i = 0; i < roleContainerList.Count; i++)
  748. {
  749. if (value == (i + 1).ToString() && roleContainerList[i].EquipIds.Count > 0)
  750. {
  751. MyDressUpHelper.dressUpObj.PutOnItemList(roleContainerList[i].EquipIds);
  752. return;
  753. }
  754. }
  755. MyDressUpHelper.dressUpObj.PutOnDefaultDressUpData(IsTeaParty);
  756. }
  757. }
  758. private void UpdateMusic(string value)
  759. {
  760. if (value.Length > 0)
  761. {
  762. if (value == "0")
  763. {
  764. MusicManager.Instance.Stop();
  765. }
  766. else
  767. {
  768. MusicManager.Instance.PlayCroutine(ResPathUtil.GetMusicPath(value, "mp3"), false, 0.1f);
  769. }
  770. }
  771. }
  772. private void PlayEffect(string[] infos)
  773. {
  774. }
  775. private void PlayShake(int[] shakeInfoArr)
  776. {
  777. if (shakeInfoArr != null && shakeInfoArr.Length > 0)
  778. {
  779. Vector3 position = _sceneObject.transform.position;
  780. position.x = (float)shakeInfoArr[0] / GameConst.PIXELS_PER_UNITY_UNIT;
  781. position.y = (float)shakeInfoArr[1] / GameConst.PIXELS_PER_UNITY_UNIT;
  782. _sceneObject.transform.position = position;
  783. float attenuationX = (float)shakeInfoArr[2] / GameConst.PIXELS_PER_UNITY_UNIT;
  784. float attenuationY = (float)shakeInfoArr[3] / GameConst.PIXELS_PER_UNITY_UNIT;
  785. float interval = (float)shakeInfoArr[4] / 1000;
  786. float duration = (float)shakeInfoArr[5] / 1000;
  787. int repeat = Mathf.RoundToInt(duration / interval);
  788. Timers.inst.Add(interval, 0, UpdateShake, new float[] { attenuationX, attenuationY });
  789. }
  790. }
  791. private void UpdateShake(object param)
  792. {
  793. float[] attenuations = param as float[];
  794. float attenuationX = attenuations[0];
  795. float attenuationY = attenuations[1];
  796. Vector3 position = _sceneObject.transform.position;
  797. bool done = false;
  798. bool doneX = false;
  799. float absX = Mathf.Abs(position.x);
  800. if (absX > attenuationX)
  801. {
  802. int dir = (int)(position.x / absX);
  803. position.x = Mathf.Abs(position.x) - attenuationX;
  804. position.x *= -1 * dir;
  805. }
  806. else
  807. {
  808. doneX = true;
  809. position.x = 0;
  810. }
  811. bool doneY = false;
  812. float absY = Mathf.Abs(position.y);
  813. if (absY > attenuationY)
  814. {
  815. int dir = (int)(position.y / absY);
  816. position.y = Mathf.Abs(position.y) - attenuationY;
  817. position.y *= -1 * dir;
  818. }
  819. else
  820. {
  821. doneY = true;
  822. position.y = 0;
  823. }
  824. done = doneX && doneY;
  825. _sceneObject.transform.position = position;
  826. if (done)
  827. {
  828. Timers.inst.Remove(UpdateShake);
  829. }
  830. }
  831. private void Over(bool isSkip = true)
  832. {
  833. if (_onCompleteStoryDialogCall != null)
  834. {
  835. _onCompleteStoryDialogCall(isSkip, _onCompleteStoryDialogCallParam);
  836. }
  837. this.Hide();
  838. }
  839. private void UpdateSpeedUpBtn()
  840. {
  841. if (_speedAutoPlay > 1)
  842. {
  843. _ui.m_btnSpeedUp.text = "x" + _speedAutoPlay;
  844. }
  845. else
  846. {
  847. _ui.m_btnSpeedUp.text = "";
  848. }
  849. _typingEffect?.SetSpeed(_speedAutoPlay);
  850. }
  851. private void StopAutoPlay()
  852. {
  853. _autoPlay = false;
  854. _ui.m_btnAutoPlay.selected = false;
  855. Timers.inst.Remove(ShowNextWords);
  856. }
  857. private IEnumerator WaitCGAnimFinish(string resName, int times = 1)
  858. {
  859. GameObject cg = PrefabManager.Instance.InstantiateSync("test");
  860. Animator animator = cg.GetComponentInChildren<Animator>();
  861. AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0);
  862. yield return new WaitForSeconds(info.length * times);
  863. //while (info.normalizedTime < 0.95)
  864. //{
  865. // info = animator.GetCurrentAnimatorStateInfo(0);
  866. // yield return new WaitForSeconds(0.1f);
  867. //}
  868. PrefabManager.Instance.Restore(cg);
  869. OnStepComplete();
  870. }
  871. /// <summary>
  872. /// 检查资源加载是否完成
  873. /// </summary>
  874. private IEnumerator CheckResLoad()
  875. {
  876. List<string> resList = new List<string>();
  877. for (int i = 0; i < _stepListToRead.Count; i++)
  878. {
  879. StoryDialogCfg cfg = _stepListToRead[i];
  880. if (cfg.bgRes.Length > 0 && cfg.bgRes != "0")
  881. {
  882. string bgRes = ResPathUtil.GetSceneBgPath(cfg.bgRes);
  883. if (!resList.Contains(bgRes))
  884. {
  885. resList.Add(bgRes);
  886. }
  887. }
  888. if (cfg.aniRes.Length > 0 && cfg.aniRes != "0")
  889. {
  890. string res = cfg.aniRes.Split('/')[1];
  891. string aniRes = ResPathUtil.GetStoryDialogCGPath(cfg.aniRes, res);
  892. if (!resList.Contains(aniRes))
  893. {
  894. resList.Add(aniRes);
  895. }
  896. }
  897. }
  898. ResourceDownloaderOperation downloaderOperation = YooAssets.CreateBundleDownloader(resList.ToArray(), 3, 3);
  899. if (downloaderOperation.TotalDownloadCount == 0)
  900. {
  901. ShowNextStep(_storyStartID);
  902. yield break;
  903. }
  904. // 开始加载资源
  905. ViewManager.Show<LoadingView>();
  906. LoadingView.Instance.SetDesc("正在加载剧情资源...");
  907. downloaderOperation.OnDownloadErrorCallback =
  908. (fileName, error) =>
  909. {
  910. Debug.LogError($"加载{fileName}失败 {error}");
  911. };
  912. downloaderOperation.OnDownloadProgressCallback =
  913. (totalDownloadCount, currentDownloadCount, totalDownloadSizeBytes, currentDownloadSizeBytes) =>
  914. {
  915. string currentSizeMB = (currentDownloadSizeBytes / 1048576f).ToString("f1");
  916. string totalSizeMB = (totalDownloadSizeBytes / 1048576f).ToString("f1");
  917. var progress = (float)currentDownloadSizeBytes / totalDownloadSizeBytes;
  918. //LauncherView.Instance.SetDesc($"正在下载资源,{currentDownloadCount}/{totalDownloadCount}", $"{currentSizeMB}MB/{totalSizeMB}MB", true);
  919. LoadingView.Instance.SetProgress((int)(progress * 100));
  920. };
  921. downloaderOperation.BeginDownload();
  922. yield return downloaderOperation;
  923. // 检测下载结果
  924. if (downloaderOperation.Status != EOperationStatus.Succeed)
  925. {
  926. Alert.Show("下载失败!请检查网络状态后重试。")
  927. .SetLeftButton(true, "返回", (data) =>
  928. {
  929. ViewManager.Hide<LoadingView>();
  930. Hide();
  931. });
  932. yield break;
  933. }
  934. // 加载完成
  935. LoadingView.Instance.SetProgress(100, () =>
  936. {
  937. ViewManager.Hide<LoadingView>();
  938. ShowNextStep(_storyStartID);
  939. }
  940. );
  941. }
  942. private RenderTexture renderTexure;
  943. private void CreateHeadImg()
  944. {
  945. // 创建render texture
  946. renderTexure = new RenderTexture(550, 760, 24);
  947. // 换装父节点 + 相机
  948. _dressUpSceneObj = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath("StoryDialogSelfImg/StoryDialogDressUpObj"));
  949. Camera camera = _dressUpSceneObj.transform.Find("Camera").GetComponent<Camera>();
  950. camera.targetTexture = renderTexure;
  951. // 创建RawImg
  952. _selfHeadImgObj = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath("StoryDialogSelfImg/Canvas"));
  953. RawImage rawImage = _selfHeadImgObj.transform.Find("mask/RawImage").GetComponent<RawImage>();
  954. rawImage.texture = renderTexure;
  955. // 将RawImg放在FGUI上
  956. GoWrapper goWrapper = new GoWrapper(_selfHeadImgObj);
  957. _ui.m_dialogHead.m_compDressUp.m_holder.SetNativeObject(goWrapper);
  958. }
  959. private void DestroyHeadImg()
  960. {
  961. // 清空render Texture
  962. Camera camera = _dressUpSceneObj.transform.Find("Camera").GetComponent<Camera>();
  963. camera.targetTexture = null;
  964. RawImage rawImage = _selfHeadImgObj.transform.Find("mask/RawImage").GetComponent<RawImage>();
  965. rawImage.texture = null;
  966. renderTexure.Release();
  967. renderTexure = null;
  968. // 回收人物
  969. _dressUpObj?.Dispose();
  970. _dressUpObj = null;
  971. // 回收RawImage
  972. if (_selfHeadImgObj != null)
  973. {
  974. PrefabManager.Instance.Restore(_selfHeadImgObj);
  975. _selfHeadImgObj = null;
  976. }
  977. // 回收相机预制
  978. if (_dressUpSceneObj != null)
  979. {
  980. PrefabManager.Instance.Restore(_dressUpSceneObj);
  981. _dressUpSceneObj = null;
  982. }
  983. }
  984. /// <summary>
  985. /// 修改主角头像组件层级
  986. /// </summary>
  987. private void ChangeHeadImgLayer()
  988. {
  989. Transform parent = _dressUpSceneObj.transform.Find("Role");
  990. ChangeLayer(parent, parent.gameObject.layer);
  991. }
  992. private void ChangeLayer(Transform transform, int layer)
  993. {
  994. if (transform.childCount > 0)
  995. {
  996. for (int i = 0; i < transform.childCount; i++)
  997. {
  998. ChangeLayer(transform.GetChild(i), layer);
  999. }
  1000. transform.gameObject.layer = layer;
  1001. }
  1002. else
  1003. {
  1004. transform.gameObject.layer = layer;
  1005. }
  1006. }
  1007. }
  1008. }