StoryDialogView.cs 40 KB

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