StoryDialogView.cs 44 KB

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