StoryDialogView.cs 45 KB

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