StoryDialogView.cs 44 KB

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