LuckyBoxController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using UI.LuckyBox;
  2. using FairyGUI;
  3. using ET;
  4. using System.Collections.Generic;
  5. using System;
  6. using UnityEngine;
  7. using System.Collections;
  8. using Live2D.Cubism.Rendering;
  9. using DG.Tweening;
  10. using System.Threading.Tasks;
  11. using cfg.GfgCfg;
  12. namespace GFGGame
  13. {
  14. public class LuckyBoxController
  15. {
  16. private GComponent _com;
  17. // private UI_ComModel _comModel;
  18. private DressUpObjUI _dressUpObjUI;
  19. private DressUpObjUI _dressUpObjUI2;
  20. private LuckyBoxCfg _luckyBoxCfg;
  21. private const int _turnTime = 7;
  22. private int _bgIndex = 0;
  23. private int _modelIndex = 0;
  24. private List<DressUpObjUI> _dressUpObjUIs = new List<DressUpObjUI>(2);
  25. private ModelInfo[] modelInfos = new ModelInfo[2];
  26. private class ModelInfo
  27. {
  28. public CubismRenderController[] cubismModels;
  29. public List<SpriteRenderer> spriteRenderers = new List<SpriteRenderer>();
  30. }
  31. public LuckyBoxController(GComponent comModel)
  32. {
  33. _dressUpObjUI = new DressUpObjUI("SceneDressUp");
  34. _dressUpObjUI2 = new DressUpObjUI("SceneDressUp");
  35. _dressUpObjUIs.Add(_dressUpObjUI);
  36. _dressUpObjUIs.Add(_dressUpObjUI2);
  37. _com = comModel;
  38. }
  39. public void OnShown(int luckyBoxId)
  40. {
  41. _luckyBoxCfg = CommonDataManager.Tables.TblLuckyBoxCfg.GetOrDefault(luckyBoxId);
  42. _com.GetChild("loaMask").asLoader.url = ResPathUtil.GetBgImgPath(_luckyBoxCfg.BgRes[_bgIndex]);
  43. InitDressUpObj();
  44. ChangeRes();
  45. Timers.inst.Remove(UpdateTime);
  46. if (_luckyBoxCfg.Res.Count > 1 || _luckyBoxCfg.SuitId.Count > 1)
  47. {
  48. Timers.inst.Add(_turnTime, 0, UpdateTime);
  49. }
  50. }
  51. private void UpdateTime(object param)
  52. {
  53. _com.GetChild("loaMask").asLoader.url = ResPathUtil.GetBgImgPath(_luckyBoxCfg.BgRes[_bgIndex]);
  54. _bgIndex = _bgIndex + 1 == _luckyBoxCfg.BgRes.Count ? 0 : _bgIndex + 1;
  55. if (_luckyBoxCfg.Res.Count > 0)
  56. {
  57. _modelIndex = _modelIndex + 1 == _luckyBoxCfg.Res.Count ? 0 : _modelIndex + 1;
  58. }
  59. else
  60. {
  61. _modelIndex = _modelIndex + 1 == _luckyBoxCfg.SuitId.Count ? 0 : _modelIndex + 1;
  62. }
  63. if (_luckyBoxCfg.IsAni == 0)
  64. {
  65. _com.GetTransition("t0").Play(ChangeRes);
  66. }
  67. else
  68. {
  69. HideModel();
  70. UpdateIndex();
  71. ChangeRes();
  72. }
  73. }
  74. private int _index = 0;
  75. private void ChangeRes()
  76. {
  77. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  78. UI_ComModelRes comRes = _comModel.m_comModelRes;
  79. _comModel.m_loaBg.url = ResPathUtil.GetBgImgPath(_luckyBoxCfg.BgRes[_bgIndex]);
  80. if (_luckyBoxCfg.Res.Count > 0)
  81. {
  82. comRes.m_loaRes.url = ResPathUtil.GetBgImgPath(_luckyBoxCfg.Res[_modelIndex]);
  83. EventAgent.DispatchEvent(ConstMessage.LUCKY_EFFECT_UPDATE, _luckyBoxCfg.Res[_modelIndex]);
  84. comRes.m_loaTitle.url = ResPathUtil.GetLuckyBoxTitlePath(_luckyBoxCfg.Res[_modelIndex]);
  85. comRes.m_loaRes.SetPosition(-110, -170, 1);
  86. int direction = _luckyBoxCfg.Scale[_modelIndex] >= 0 ? 1 : -1;
  87. float scale = _luckyBoxCfg.Scale.Count > 0 ? Math.Abs(_luckyBoxCfg.Scale[_modelIndex]) / 10000f : 1;
  88. comRes.m_loaRes.SetScale(direction * scale, scale);
  89. }
  90. else if (_luckyBoxCfg.SuitId.Count > 0)
  91. {
  92. int direction = 1;
  93. if (_luckyBoxCfg.Scale.Count > 0)
  94. {
  95. direction = _luckyBoxCfg.Scale[_modelIndex] >= 0 ? 1 : -1;
  96. }
  97. if (_luckyBoxCfg.IsAni == 0)
  98. {
  99. _comModel.m_loaMask.visible = true;
  100. SuitCfg suitCfg =
  101. CommonDataManager.Tables.TblSuitCfg.GetOrDefault(_luckyBoxCfg.SuitId[_modelIndex]);
  102. comRes.m_loaRes.url = ResPathUtil.GetDressUpPath(suitCfg.AniRes);
  103. float scale = _luckyBoxCfg.Scale.Count > 0 ? Math.Abs(_luckyBoxCfg.Scale[_modelIndex]) / 10000f : 1;
  104. comRes.m_loaRes.SetScale(direction * scale, scale);
  105. comRes.m_loaRes.SetPosition(_luckyBoxCfg.Pos[_modelIndex].Key, _luckyBoxCfg.Pos[_modelIndex].Val,
  106. 1);
  107. }
  108. else
  109. {
  110. _comModel.m_loaMask.visible = false;
  111. comRes = (_index == 0 ? _comModel.m_comModelRes : _comModel.m_comModelRes2);
  112. int scale = _luckyBoxCfg.Scale.Count > 0 ? Math.Abs(_luckyBoxCfg.Scale[_modelIndex]) / 10000 : 100;
  113. _comModel.m_loaMask.alpha = 1;
  114. comRes.m_loaRes.url = "";
  115. if (modelInfos[_index] == null)
  116. {
  117. modelInfos[_index] = new ModelInfo();
  118. _dressUpObjUIs[_index].UpdateWrapper(comRes.m_holder);
  119. comRes.m_holder.SetPosition(_luckyBoxCfg.Pos[_modelIndex].Key,
  120. _luckyBoxCfg.Pos[_modelIndex].Val, 1);
  121. // 使用异步方式重置场景对象
  122. _dressUpObjUIs[_index].ResetSceneObjAsync(
  123. scale,
  124. false,
  125. true,
  126. null,
  127. false,
  128. (sceneObj) =>
  129. {
  130. if (sceneObj != null)
  131. {
  132. // 场景对象加载完成后穿上套装
  133. _dressUpObjUIs[_index].dressUpObj.PutOnSuitCfg(
  134. _luckyBoxCfg.SuitId[_modelIndex],
  135. true,
  136. new int[] { ConstDressUpItemType.BEI_JING },
  137. false,
  138. false
  139. );
  140. Timers.inst.StartCoroutine(DelayShowModel(direction));
  141. }
  142. else
  143. {
  144. Debug.LogError("Failed to load scene object for lucky box");
  145. }
  146. }
  147. );
  148. }
  149. else
  150. {
  151. ShowModel();
  152. }
  153. }
  154. }
  155. if (_luckyBoxCfg.IsAni == 0)
  156. {
  157. _com.GetTransition("t1").Play();
  158. }
  159. UI_ComModel.ProxyEnd();
  160. }
  161. private IEnumerator DelayShowModel(int direction)
  162. {
  163. _dressUpObjUIs[_index].sceneObject.transform.localScale = Vector3.zero;
  164. // 未加载到live2D之前,不做显示
  165. while (!InitModel(_index))
  166. {
  167. yield return new WaitForEndOfFrame();
  168. }
  169. int scale = _luckyBoxCfg.Scale.Count > 0 ? Math.Abs(_luckyBoxCfg.Scale[_modelIndex]) / 10000 : 100;
  170. _dressUpObjUIs[_index].sceneObject.transform.localScale = new Vector3(direction * scale, scale, scale);
  171. ShowModel();
  172. }
  173. public void OnHide()
  174. {
  175. DOTween.Kill("fade");
  176. _bgIndex = 0;
  177. _modelIndex = 0;
  178. _dressUpObjUI.dressUpObj.TakeOffAll();
  179. ChangeModelAlpha(0, 1);
  180. ChangeModelAlpha(1, 1);
  181. // 隐藏另一个模型
  182. if (_dressUpObjUIs[0].sceneObject != null)
  183. {
  184. _dressUpObjUIs[0].sceneObject.transform.localScale = Vector3.zero;
  185. }
  186. if (_dressUpObjUIs[1].sceneObject != null)
  187. {
  188. _dressUpObjUIs[1].sceneObject.transform.localScale = Vector3.zero;
  189. }
  190. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  191. _comModel.m_comModelRes.m_loaRes.url = "";
  192. _comModel.m_comModelRes.m_loaTitle.url = "";
  193. UI_ComModel.ProxyEnd();
  194. Timers.inst.Remove(UpdateTime);
  195. }
  196. public void Dispose()
  197. {
  198. if (_dressUpObjUI != null)
  199. {
  200. _dressUpObjUI.Dispose();
  201. _dressUpObjUI = null;
  202. }
  203. if (_dressUpObjUI2 != null)
  204. {
  205. _dressUpObjUI2.Dispose();
  206. _dressUpObjUI2 = null;
  207. }
  208. _dressUpObjUIs.Clear();
  209. }
  210. private void UpdateIndex()
  211. {
  212. _index ^= 1;
  213. }
  214. private bool InitModel(int index)
  215. {
  216. if (index >= _dressUpObjUIs.Count || _dressUpObjUIs[index].sceneObject == null)
  217. {
  218. return false;
  219. }
  220. Transform model = _dressUpObjUIs[index].sceneObject.transform.Find("Role");
  221. modelInfos[index].cubismModels = model.GetComponentsInChildren<CubismRenderController>();
  222. modelInfos[index].spriteRenderers =
  223. new List<SpriteRenderer>(model.GetComponentsInChildren<SpriteRenderer>());
  224. return modelInfos[index].cubismModels.Length > 0;
  225. }
  226. private void ShowModel()
  227. {
  228. ChangeModelAlphaAnim(_index, 0, 1);
  229. Transform obj = _dressUpObjUIs[_index].sceneObject.transform;
  230. if (obj != null)
  231. {
  232. obj.gameObject.SetActive(true);
  233. }
  234. }
  235. private void HideModel()
  236. {
  237. InitModel(_index);
  238. ChangeModelAlphaAnim(_index, 1, 0);
  239. Transform obj = _dressUpObjUIs[_index].sceneObject.transform;
  240. if (obj != null)
  241. {
  242. obj.gameObject.SetActive(false);
  243. }
  244. }
  245. private void ChangeComResLayer()
  246. {
  247. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  248. int index = _comModel.target.GetChildIndex(_comModel.m_comModelRes.target);
  249. int index2 = _comModel.target.GetChildIndex(_comModel.m_comModelRes2.target);
  250. _comModel.target.SetChildIndex(_comModel.m_comModelRes.target, index2);
  251. _comModel.target.SetChildIndex(_comModel.m_comModelRes2.target, index);
  252. UI_ComModel.ProxyEnd();
  253. }
  254. private void ChangeModelAlpha(int index, float value)
  255. {
  256. if (modelInfos[index] == null || modelInfos[index].cubismModels == null)
  257. {
  258. return;
  259. }
  260. for (int j = 0; j < modelInfos[index].cubismModels.Length; j++)
  261. {
  262. modelInfos[index].cubismModels[j].Opacity = value;
  263. }
  264. for (int i = 0; i < modelInfos[index].spriteRenderers.Count; i++)
  265. {
  266. if (modelInfos[index].spriteRenderers[i] == null)
  267. {
  268. modelInfos[index].spriteRenderers.RemoveAt(i);
  269. continue;
  270. }
  271. Color color = modelInfos[index].spriteRenderers[i].color;
  272. color.a = value;
  273. modelInfos[index].spriteRenderers[i].color = color;
  274. }
  275. }
  276. private void ChangeModelAlphaAnim(int index, float start, float end, Action action = null)
  277. {
  278. Ease ease = Ease.Linear;
  279. DOTween.To(() => start, (value) => { ChangeModelAlpha(index, value); }, end, 0.3f).SetEase(ease)
  280. .OnComplete(() => { action?.Invoke(); }).SetAutoKill().SetId("fade");
  281. }
  282. private void InitDressUpObj()
  283. {
  284. modelInfos[0] = null;
  285. modelInfos[1] = null;
  286. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  287. _dressUpObjUI.UpdateWrapper(_comModel.m_comModelRes.m_holder);
  288. _dressUpObjUI2.UpdateWrapper(_comModel.m_comModelRes2.m_holder);
  289. UI_ComModel.ProxyEnd();
  290. }
  291. }
  292. }