StoryDialogView.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. EffectUIPool.CreateEffectUI(_ui.m_dialogHead.m_comphead.m_holder, "ui_nzbq",
  554. headAniCfg.HeadAni,
  555. onComplete: (effect) =>
  556. {
  557. if (effect != null)
  558. {
  559. _effectUI1 = effect;
  560. }
  561. });
  562. }
  563. else
  564. {
  565. //换装
  566. _ui.m_dialogHead.m_comphead.target.visible = false;
  567. //_dressUpObjUI.ResetSceneObj(80, true, false, sceneObject.transform.Find("Scene").gameObject, false);
  568. GRoot.inst.touchable = false;
  569. _dressUpObj.setSceneObj(_dressUpSceneObj, false, false, null, false, ChangeHeadImgLayer);
  570. if (storyDialogCfg.SuitId > 0)
  571. {
  572. _dressUpObj.PutOnSuitCfg(storyDialogCfg.SuitId, false,
  573. new int[] { ConstDressUpItemType.SHOU_CHI_WU }, false, false);
  574. }
  575. else
  576. {
  577. _dressUpObj.PutOnDressUpData(CustomSuitDataManager.GetCurrentSuitData().dressUpData,
  578. new int[] { ConstDressUpItemType.SHOU_CHI_WU });
  579. if (_dressUpObj.actionId > 0)
  580. {
  581. _dressUpObj.CancelAction(true, new int[] { ConstDressUpItemType.SHOU_CHI_WU });
  582. }
  583. }
  584. if (headAniCfg != null && headAniCfg.FaceId > 0)
  585. {
  586. //表情
  587. _dressUpObj.AddOrRemove(headAniCfg.FaceId, true);
  588. }
  589. _ui.m_dialogHead.m_compDressUp.target.visible = true;
  590. }
  591. _ui.m_dialogHead.target.visible = true;
  592. _wordTextField = _ui.m_dialogHead.m_txtContent;
  593. _arrow = _ui.m_dialogHead.m_iconNext;
  594. lastTextFieldType = "head";
  595. }
  596. else if (!string.IsNullOrEmpty(headRes))
  597. {
  598. _ui.m_dialogHead.target.visible = true;
  599. _ui.m_dialogHead.m_txtName.text = roleName;
  600. _ui.m_dialogHead.m_comphead.m_head.visible = true;
  601. _ui.m_dialogHead.m_comphead.m_holder.visible = false;
  602. _ui.m_dialogHead.m_comphead.m_head.url = ResPathUtil.GetNpcHeadPath(headRes);
  603. _wordTextField = _ui.m_dialogHead.m_txtContent;
  604. _arrow = _ui.m_dialogHead.m_iconNext;
  605. lastTextFieldType = "head";
  606. }
  607. else if (!string.IsNullOrEmpty(roleName))
  608. {
  609. _ui.m_dialogName.target.visible = true;
  610. if (IsTeaParty)
  611. {
  612. var roleContainerList = LeagueDataManager.Instance.RoleContainerList;
  613. var teapartyRoleCfg =
  614. CommonDataManager.Tables.TblTeapartyRoleCfg.GetGroup1ById(LeagueDataManager.Instance
  615. .TeaPartyId);
  616. int roleIndex = Convert.ToInt32(roleName);
  617. _ui.m_dialogName.m_txtName.text = teapartyRoleCfg[roleIndex - 1].Name;
  618. _ui.m_dialogName.m_bgType.selectedIndex = 1;
  619. _ui.m_dialogName.m_showArrow.selectedIndex = 1;
  620. if (roleIndex <= roleContainerList.Count &&
  621. roleContainerList[roleIndex - 1].MaxScoreRoleName != null &&
  622. roleContainerList[roleIndex - 1].MaxScoreRoleName != "" &&
  623. _ui.m_dialogName.m_txtName.text != "")
  624. {
  625. _ui.m_comPlayName.visible = true;
  626. _ui.m_txtPlayName.text = roleContainerList[roleIndex - 1].MaxScoreRoleName;
  627. if (_ui.m_txtPlayName.text == RoleDataManager.roleName)
  628. _ui.m_dialogName.m_bgType.selectedIndex = 0;
  629. }
  630. else
  631. _ui.m_comPlayName.visible = false;
  632. }
  633. else
  634. {
  635. _ui.m_dialogName.m_bgType.selectedIndex = 0;
  636. _ui.m_dialogName.m_showArrow.selectedIndex = 0;
  637. _ui.m_dialogName.m_txtName.text = roleName;
  638. }
  639. _wordTextField = _ui.m_dialogName.m_txtContent;
  640. _arrow = _ui.m_dialogName.m_iconNext;
  641. if (!lastTextFieldType.Equals("name"))
  642. {
  643. lastTextFieldType = "name";
  644. StoryDialogDataManager.Instance.waiting = true;
  645. _ui.m_t0.Play(() => { StoryDialogDataManager.Instance.waiting = false; });
  646. }
  647. }
  648. else
  649. {
  650. if (IsTeaParty)
  651. _ui.m_dialogText.m_showArrow.selectedIndex = 1;
  652. else
  653. _ui.m_dialogText.m_showArrow.selectedIndex = 0;
  654. _ui.m_dialogText.target.visible = true;
  655. _wordTextField = _ui.m_dialogText.m_txtContent;
  656. _arrow = _ui.m_dialogText.m_iconNext;
  657. lastTextFieldType = "text";
  658. _ui.m_comPlayName.visible = false;
  659. }
  660. _wordIndex = 0;
  661. _typingEffect = new TypingFadeEffectPro(_wordTextField);
  662. _typingEffect.typeFinishedAction = ShowCurrentWords;
  663. ShowNextDialog();
  664. }
  665. else
  666. {
  667. OnStepComplete();
  668. //if (effectInfo.Length > 0)
  669. //{
  670. // // 等待CG播放完毕直接进入下一段
  671. // Timers.inst.StartCoroutine(WaitCGAnimFinish(effectInfo[0], int.Parse(effectInfo[1])));
  672. //}
  673. //// 播放效果等
  674. //else
  675. //{
  676. // OnStepComplete();
  677. //}
  678. }
  679. }
  680. /// <summary>
  681. /// 延迟显示头像,防止换装异步加载/换表情 未完成时显示了头像
  682. /// </summary>
  683. private async void ShowSelfHeadImg()
  684. {
  685. _ui.m_dialogHead.m_compDressUp.target.visible = false;
  686. await Task.Delay(80);
  687. _ui.m_dialogHead.m_compDressUp.target.visible = true;
  688. }
  689. private void ShowNextDialog()
  690. {
  691. if (_wordList != null && _wordList.Length > _wordIndex)
  692. {
  693. string itemInfo = _wordList[_wordIndex];
  694. string[] itemInfoList = Regex.Split(itemInfo, "=");
  695. _currentWords = itemInfoList[0];
  696. if (itemInfoList.Length > 1)
  697. {
  698. _wordTextField.data = itemInfoList[1];
  699. }
  700. else
  701. {
  702. _wordTextField.data = null;
  703. }
  704. Timers.inst.StartCoroutine(StartShowLetters());
  705. }
  706. else
  707. {
  708. OnStepComplete();
  709. }
  710. }
  711. private void ShowCurrentWords()
  712. {
  713. //_arrow.target.visible = true;
  714. //Timers.inst.Remove(UpdateLetters);
  715. StopTyping();
  716. _typingEffect?.Cancel();
  717. _wordTextField.text = _currentWords;
  718. _dialogListLookBack.Add(_currentWords);
  719. _isShowLetters = false;
  720. _wordIndex++;
  721. if (_autoPlay)
  722. {
  723. float interval = Mathf.Max(VoiceManager.Instance.GetClipRemainingLength(),
  724. GameConst.NEXT_WORDS_INTERVAL_MAX / _speedAutoPlay);
  725. Timers.inst.Add(interval, 1, ShowNextWords);
  726. }
  727. }
  728. private void ShowNextWords(object param = null)
  729. {
  730. if (_wordTextField != null)
  731. {
  732. if (_isShowLetters)
  733. {
  734. ShowCurrentWords();
  735. }
  736. else
  737. {
  738. string stepID = (string)_wordTextField.data;
  739. if (stepID != null)
  740. {
  741. OnStepComplete(stepID);
  742. }
  743. else
  744. {
  745. ShowNextDialog();
  746. }
  747. }
  748. }
  749. }
  750. private IEnumerator StartShowLetters()
  751. {
  752. _isShowLetters = true;
  753. _arrow.target.visible = false;
  754. _wordTextField.verticalAlign = VertAlignType.Top;
  755. _wordTextField.text = "";
  756. _canClickBtnNext = false;
  757. while (StoryDialogDataManager.Instance.waitPicFade || StoryDialogDataManager.Instance.waiting)
  758. {
  759. //Debug.Log("等待立绘/动画结束");
  760. yield return new WaitForEndOfFrame();
  761. }
  762. _canClickBtnNext = true;
  763. _wordTextField.text = _currentWords;
  764. StartTyping();
  765. }
  766. /// <summary>
  767. /// 开启打字机显示
  768. /// </summary>
  769. private void StartTyping()
  770. {
  771. _typingEffect.SetSpeed(_speedAutoPlay);
  772. _typingEffect.Start();
  773. // 如果配置了语音,则播放语音
  774. VoiceManager.Instance.PlayVoice();
  775. }
  776. private void StopTyping()
  777. {
  778. _typingEffect.Cancel();
  779. }
  780. private void UpdateBg(string value)
  781. {
  782. if (value.Length > 0)
  783. {
  784. SceneController.UpdateDialogBg(value, _sceneObject);
  785. }
  786. }
  787. private void UpdatePic(string value, string faceValue = "", string emojiValue = "")
  788. {
  789. if (IsTeaParty) return;
  790. // 同步处理 value
  791. if (value.Length > 0)
  792. {
  793. SceneController.UpdateDialogPic(value, _sceneObject);
  794. }
  795. // 异步处理 faceValue
  796. if (faceValue.Length > 0)
  797. {
  798. SceneController.InitPicFaceAsync(faceValue, _sceneObject, () =>
  799. {
  800. // faceValue 加载完成后处理 emojiValue
  801. if (emojiValue.Length > 0)
  802. {
  803. UpdateLiveEmoji(emojiValue);
  804. }
  805. });
  806. }
  807. else if (emojiValue.Length > 0) // 如果不需要加载 faceValue,直接处理 emoji
  808. {
  809. UpdateLiveEmoji(emojiValue);
  810. }
  811. }
  812. private void UpdateLiveEmoji(string EmojiRes)
  813. {
  814. if (EmojiRes.Length > 0 && !IsTeaParty)
  815. {
  816. SceneController.UpdateEmojiAni(EmojiRes, _animObject);
  817. }
  818. }
  819. private void UpdateAnim(string value)
  820. {
  821. if (value.Length > 0 && !IsTeaParty)
  822. {
  823. if (value == "0")
  824. {
  825. //SceneController.ControlBgVisible(_sceneObject, true);
  826. _ui.m_hideAnimMask.Play();
  827. SceneController.UpdateDialogAnim(value, _animObject);
  828. }
  829. // 播放动画
  830. else
  831. {
  832. SceneController.UpdateDialogAnim(value, _animObject);
  833. _animObject.SetActive(false);
  834. _canClickBtnNext = false;
  835. _ui.m_showMask.Play(() =>
  836. {
  837. //_canClickBtnNext = true;
  838. if (_animObject != null)
  839. {
  840. _ui.m_hideMask.Play();
  841. _ui.m_showAnimMask.Play();
  842. _animObject.SetActive(true);
  843. }
  844. //SceneController.ControlBgVisible(_sceneObject, false);
  845. });
  846. }
  847. }
  848. }
  849. private void UpdateRoleObj(string value)
  850. {
  851. if (IsTeaParty)
  852. {
  853. var roleContainerList = LeagueDataManager.Instance.RoleContainerList;
  854. MyDressUpHelper.dressUpObj.setSceneObj(_sceneObject, false, false);
  855. for (int i = 0; i < roleContainerList.Count; i++)
  856. {
  857. if (value == (i + 1).ToString() && roleContainerList[i].EquipIds.Count > 0)
  858. {
  859. MyDressUpHelper.dressUpObj.PutOnItemList(roleContainerList[i].EquipIds);
  860. return;
  861. }
  862. }
  863. MyDressUpHelper.dressUpObj.PutOnDefaultDressUpData(IsTeaParty);
  864. }
  865. }
  866. private void UpdateMusic(string value)
  867. {
  868. if (value.Length > 0)
  869. {
  870. if (value == "0")
  871. {
  872. MusicManager.Instance.Stop();
  873. }
  874. else
  875. {
  876. MusicManager.Instance.PlayCroutine(ResPathUtil.GetMusicPath(value, "mp3"), false, 0.1f);
  877. }
  878. }
  879. }
  880. private void PlayEffect(List<string> value)
  881. {
  882. if (value.Count > 0)
  883. {
  884. if (value[0] == "0")
  885. {
  886. EffectUIPool.Recycle(_effect);
  887. _effect = null;
  888. }
  889. else
  890. {
  891. EffectUIPool.Recycle(_effect);
  892. _effect = null;
  893. string effectPath = string.Format("ui_Juqing/{0}", value[0]);
  894. EffectUIPool.CreateEffectUI(_ui.m_effect, effectPath, value[0],
  895. onComplete: (effect) =>
  896. {
  897. if (effect != null)
  898. {
  899. _effect = effect;
  900. }
  901. });
  902. _ui.m_effect.visible = true;
  903. }
  904. }
  905. }
  906. private void PlayShake(List<int> shakeInfoArr)
  907. {
  908. if (shakeInfoArr != null && shakeInfoArr.Count > 0)
  909. {
  910. Vector3 position = _sceneObject.transform.position;
  911. position.x = (float)shakeInfoArr[0] / GameConst.PIXELS_PER_UNITY_UNIT;
  912. position.y = (float)shakeInfoArr[1] / GameConst.PIXELS_PER_UNITY_UNIT;
  913. _sceneObject.transform.position = position;
  914. float attenuationX = (float)shakeInfoArr[2] / GameConst.PIXELS_PER_UNITY_UNIT;
  915. float attenuationY = (float)shakeInfoArr[3] / GameConst.PIXELS_PER_UNITY_UNIT;
  916. float interval = (float)shakeInfoArr[4] / 1000;
  917. float duration = (float)shakeInfoArr[5] / 1000;
  918. int repeat = Mathf.RoundToInt(duration / interval);
  919. Timers.inst.Add(interval, 0, UpdateShake, new float[] { attenuationX, attenuationY });
  920. }
  921. }
  922. private void UpdateShake(object param)
  923. {
  924. float[] attenuations = param as float[];
  925. float attenuationX = attenuations[0];
  926. float attenuationY = attenuations[1];
  927. Vector3 position = _sceneObject.transform.position;
  928. bool done = false;
  929. bool doneX = false;
  930. float absX = Mathf.Abs(position.x);
  931. if (absX > attenuationX)
  932. {
  933. int dir = (int)(position.x / absX);
  934. position.x = Mathf.Abs(position.x) - attenuationX;
  935. position.x *= -1 * dir;
  936. }
  937. else
  938. {
  939. doneX = true;
  940. position.x = 0;
  941. }
  942. bool doneY = false;
  943. float absY = Mathf.Abs(position.y);
  944. if (absY > attenuationY)
  945. {
  946. int dir = (int)(position.y / absY);
  947. position.y = Mathf.Abs(position.y) - attenuationY;
  948. position.y *= -1 * dir;
  949. }
  950. else
  951. {
  952. doneY = true;
  953. position.y = 0;
  954. }
  955. done = doneX && doneY;
  956. _sceneObject.transform.position = position;
  957. if (done)
  958. {
  959. Timers.inst.Remove(UpdateShake);
  960. }
  961. }
  962. private void Over(bool done = true)
  963. {
  964. if (_onCompleteStoryDialogCall != null)
  965. {
  966. _onCompleteStoryDialogCall(done, _onCompleteStoryDialogCallParam);
  967. }
  968. this.Hide();
  969. }
  970. private void UpdateSpeedUpBtn()
  971. {
  972. if (_speedAutoPlay > 1)
  973. {
  974. _ui.m_btnSpeedUp.text = "x" + _speedAutoPlay;
  975. }
  976. else
  977. {
  978. _ui.m_btnSpeedUp.text = "";
  979. }
  980. _typingEffect?.SetSpeed(_speedAutoPlay);
  981. }
  982. private void StopAutoPlay()
  983. {
  984. _autoPlay = false;
  985. _ui.m_btnAutoPlay.selected = false;
  986. Timers.inst.Remove(ShowNextWords);
  987. }
  988. // private IEnumerator WaitCGAnimFinish(string resName, int times = 1)
  989. // {
  990. // GameObject cg = PrefabManager.Instance.InstantiateSync("test");
  991. // Animator animator = cg.GetComponentInChildren<Animator>();
  992. // AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0);
  993. //
  994. // yield return new WaitForSeconds(info.length * times);
  995. // //while (info.normalizedTime < 0.95)
  996. // //{
  997. // // info = animator.GetCurrentAnimatorStateInfo(0);
  998. // // yield return new WaitForSeconds(0.1f);
  999. // //}
  1000. //
  1001. // PrefabManager.Instance.Restore(cg);
  1002. // OnStepComplete();
  1003. // }
  1004. /// <summary>
  1005. /// 检查资源加载是否完成
  1006. /// </summary>
  1007. private IEnumerator CheckResLoad()
  1008. {
  1009. List<string> resList = new List<string>();
  1010. for (int i = 0; i < _stepListToRead.Count; i++)
  1011. {
  1012. StoryDialogCfg cfg = _stepListToRead[i];
  1013. if (cfg.BgRes.Length > 0 && cfg.BgRes != "0")
  1014. {
  1015. string bgRes = ResPathUtil.GetSceneBgPath(cfg.BgRes);
  1016. if (!resList.Contains(bgRes))
  1017. {
  1018. resList.Add(bgRes);
  1019. }
  1020. }
  1021. if (cfg.AniRes.Length > 0 && cfg.AniRes != "0")
  1022. {
  1023. string res = cfg.AniRes.Split('/')[1];
  1024. string aniRes = ResPathUtil.GetStoryDialogCGPath(cfg.AniRes, res);
  1025. if (!resList.Contains(aniRes))
  1026. {
  1027. resList.Add(aniRes);
  1028. }
  1029. }
  1030. if (cfg.EmojiRes.Length > 0 && cfg.EmojiRes != "0")
  1031. {
  1032. string EmojiPath = string.Format("DressUp/{0}", cfg.EmojiRes);
  1033. string aniRes = ResPathUtil.GetEmojiResPath(EmojiPath, cfg.EmojiRes);
  1034. if (!resList.Contains(aniRes))
  1035. {
  1036. resList.Add(aniRes);
  1037. }
  1038. }
  1039. }
  1040. // 异步加载资源
  1041. ResourceDownloaderOperation downloaderOperation = YooAssets.CreateBundleDownloader(resList.ToArray(), 3, 3);
  1042. if (downloaderOperation.TotalDownloadCount == 0)
  1043. {
  1044. ShowNextStep(_storyStartID);
  1045. yield break;
  1046. }
  1047. ViewManager.Show<LoadingView>();
  1048. LoadingView.Instance.SetDesc("正在加载剧情资源...");
  1049. // 开始异步下载
  1050. downloaderOperation.BeginDownload();
  1051. yield return downloaderOperation;
  1052. // 检测下载结果
  1053. if (downloaderOperation.Status != EOperationStatus.Succeed)
  1054. {
  1055. Alert.Show("下载失败!请检查网络状态后重试。")
  1056. .SetLeftButton(true, "返回", (data) =>
  1057. {
  1058. ViewManager.Hide<LoadingView>();
  1059. Hide();
  1060. });
  1061. yield break;
  1062. }
  1063. // 加载完成
  1064. LoadingView.Instance.SetProgress(100, () =>
  1065. {
  1066. ViewManager.Hide<LoadingView>();
  1067. ShowNextStep(_storyStartID);
  1068. });
  1069. }
  1070. private RenderTexture renderTexure;
  1071. private void CreateHeadImg()
  1072. {
  1073. // 创建render texture
  1074. renderTexure = new RenderTexture(550, 760, 24);
  1075. // 异步加载换装父节点 + 相机
  1076. PrefabManager.Instance.InstantiateAsync(
  1077. ResPathUtil.GetPrefabPath("StoryDialogSelfImg/StoryDialogDressUpObj"),
  1078. (go) =>
  1079. {
  1080. if (go != null)
  1081. {
  1082. _dressUpSceneObj = go;
  1083. Camera camera = _dressUpSceneObj.transform.Find("Camera").GetComponent<Camera>();
  1084. camera.targetTexture = renderTexure;
  1085. // 异步加载RawImg
  1086. PrefabManager.Instance.InstantiateAsync(
  1087. ResPathUtil.GetPrefabPath("StoryDialogSelfImg/Canvas"),
  1088. (canvasGo) =>
  1089. {
  1090. if (canvasGo != null)
  1091. {
  1092. _selfHeadImgObj = canvasGo;
  1093. RawImage rawImage = _selfHeadImgObj.transform.Find("mask/RawImage")
  1094. .GetComponent<RawImage>();
  1095. rawImage.texture = renderTexure;
  1096. // 将RawImg放在FGUI上
  1097. GoWrapper goWrapper = new GoWrapper(_selfHeadImgObj);
  1098. _ui.m_dialogHead.m_compDressUp.m_holder.SetNativeObject(goWrapper);
  1099. }
  1100. else
  1101. {
  1102. Debug.LogError("Failed to load Canvas prefab");
  1103. }
  1104. }
  1105. );
  1106. }
  1107. else
  1108. {
  1109. Debug.LogError("Failed to load DressUpObj prefab");
  1110. }
  1111. }
  1112. );
  1113. }
  1114. private void DestroyHeadImg()
  1115. {
  1116. // 清空render Texture
  1117. Camera camera = _dressUpSceneObj.transform.Find("Camera").GetComponent<Camera>();
  1118. camera.targetTexture = null;
  1119. RawImage rawImage = _selfHeadImgObj.transform.Find("mask/RawImage").GetComponent<RawImage>();
  1120. rawImage.texture = null;
  1121. renderTexure.Release();
  1122. renderTexure = null;
  1123. // 回收人物
  1124. _dressUpObj?.Dispose();
  1125. _dressUpObj = null;
  1126. // 回收RawImage
  1127. if (_selfHeadImgObj != null)
  1128. {
  1129. PrefabManager.Instance.Restore(_selfHeadImgObj);
  1130. _selfHeadImgObj = null;
  1131. }
  1132. // 回收相机预制
  1133. if (_dressUpSceneObj != null)
  1134. {
  1135. PrefabManager.Instance.Restore(_dressUpSceneObj);
  1136. _dressUpSceneObj = null;
  1137. }
  1138. }
  1139. /// <summary>
  1140. /// 修改主角头像组件层级
  1141. /// </summary>
  1142. private void ChangeHeadImgLayer()
  1143. {
  1144. if (_dressUpSceneObj == null)
  1145. {
  1146. return;
  1147. }
  1148. Transform parent = _dressUpSceneObj.transform.Find("Role");
  1149. // HeadImg的层级:6
  1150. ChangeLayer(parent, 6);
  1151. GRoot.inst.touchable = true;
  1152. }
  1153. private void ChangeLayer(Transform transform, int layer)
  1154. {
  1155. if (transform == null)
  1156. {
  1157. return;
  1158. }
  1159. if (transform.childCount > 0)
  1160. {
  1161. for (int i = 0; i < transform.childCount; i++)
  1162. {
  1163. ChangeLayer(transform.GetChild(i), layer);
  1164. }
  1165. transform.gameObject.layer = layer;
  1166. }
  1167. else
  1168. {
  1169. transform.gameObject.layer = layer;
  1170. }
  1171. }
  1172. }
  1173. }