StoryDialogView.cs 43 KB

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