LuckyBoxController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 = string.Format("ui://LuckyBox/{0}", _luckyBoxCfg.resArr[_modelIndex]);
  83. comRes.m_loaRes.url = ResPathUtil.GetBgImgPath(_luckyBoxCfg.Res[_modelIndex]);
  84. EventAgent.DispatchEvent(ConstMessage.LUCKY_EFFECT_UPDATE, _luckyBoxCfg.Res[_modelIndex]);
  85. comRes.m_loaTitle.url = ResPathUtil.GetLuckyBoxTitlePath(_luckyBoxCfg.Res[_modelIndex]);
  86. comRes.m_loaRes.SetPosition(-110, -170, 1);
  87. int direction = _luckyBoxCfg.Scale[_modelIndex] >= 0 ? 1 : -1;
  88. float scale = _luckyBoxCfg.Scale.Count > 0 ? Math.Abs(_luckyBoxCfg.Scale[_modelIndex]) / 10000f : 1;
  89. comRes.m_loaRes.SetScale(direction * scale, scale);
  90. }
  91. else if (_luckyBoxCfg.SuitId.Count > 0)
  92. {
  93. int direction = 1;
  94. if (_luckyBoxCfg.Scale.Count > 0)
  95. {
  96. direction = _luckyBoxCfg.Scale[_modelIndex] >= 0 ? 1 : -1;
  97. }
  98. if (_luckyBoxCfg.IsAni == 0)
  99. {
  100. _comModel.m_loaMask.visible = true;
  101. SuitCfg suitCfg = 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, 1);
  106. }
  107. else
  108. {
  109. _comModel.m_loaMask.visible = false;
  110. comRes = (_index == 0 ? _comModel.m_comModelRes : _comModel.m_comModelRes2);
  111. int scale = _luckyBoxCfg.Scale.Count > 0 ? Math.Abs(_luckyBoxCfg.Scale[_modelIndex]) / 10000 : 100;
  112. _comModel.m_loaMask.alpha = 1;
  113. comRes.m_loaRes.url = "";
  114. if(modelInfos[_index] == null)
  115. {
  116. modelInfos[_index] = new ModelInfo();
  117. _dressUpObjUIs[_index].UpdateWrapper(comRes.m_holder);
  118. comRes.m_holder.SetPosition(_luckyBoxCfg.Pos[_modelIndex].Key, _luckyBoxCfg.Pos[_modelIndex].Val, 1);
  119. _dressUpObjUIs[_index].ResetSceneObj(scale, false, true, null, false);
  120. _dressUpObjUIs[_index].dressUpObj.PutOnSuitCfg(_luckyBoxCfg.SuitId[_modelIndex], true, new int[] { ConstDressUpItemType.BEI_JING }, false, false);
  121. Timers.inst.StartCoroutine(DelayShowModel(direction));
  122. }
  123. else
  124. {
  125. ShowModel();
  126. }
  127. }
  128. }
  129. if (_luckyBoxCfg.IsAni == 0)
  130. {
  131. _com.GetTransition("t1").Play();
  132. }
  133. UI_ComModel.ProxyEnd();
  134. }
  135. private IEnumerator DelayShowModel(int direction)
  136. {
  137. _dressUpObjUIs[_index].sceneObject.transform.localScale = Vector3.zero;
  138. // 未加载到live2D之前,不做显示
  139. while (!InitModel(_index))
  140. {
  141. yield return new WaitForEndOfFrame();
  142. }
  143. int scale = _luckyBoxCfg.Scale.Count > 0 ? Math.Abs(_luckyBoxCfg.Scale[_modelIndex]) / 10000 : 100;
  144. _dressUpObjUIs[_index].sceneObject.transform.localScale = new Vector3(direction * scale, scale, scale);
  145. ShowModel();
  146. }
  147. public void OnHide()
  148. {
  149. DOTween.Kill("fade");
  150. _bgIndex = 0;
  151. _modelIndex = 0;
  152. _dressUpObjUI.dressUpObj.TakeOffAll();
  153. ChangeModelAlpha(0, 1);
  154. ChangeModelAlpha(1, 1);
  155. // 隐藏另一个模型
  156. if (_dressUpObjUIs[0].sceneObject != null)
  157. {
  158. _dressUpObjUIs[0].sceneObject.transform.localScale = Vector3.zero;
  159. }
  160. if (_dressUpObjUIs[1].sceneObject != null)
  161. {
  162. _dressUpObjUIs[1].sceneObject.transform.localScale = Vector3.zero;
  163. }
  164. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  165. _comModel.m_comModelRes.m_loaRes.url = "";
  166. _comModel.m_comModelRes.m_loaTitle.url = "";
  167. UI_ComModel.ProxyEnd();
  168. Timers.inst.Remove(UpdateTime);
  169. }
  170. public void Dispose()
  171. {
  172. if (_dressUpObjUI != null)
  173. {
  174. _dressUpObjUI.Dispose();
  175. _dressUpObjUI = null;
  176. }
  177. if (_dressUpObjUI2 != null)
  178. {
  179. _dressUpObjUI2.Dispose();
  180. _dressUpObjUI2 = null;
  181. }
  182. _dressUpObjUIs.Clear();
  183. }
  184. private void UpdateIndex()
  185. {
  186. _index ^= 1;
  187. }
  188. private bool InitModel(int index)
  189. {
  190. if (index >= _dressUpObjUIs.Count || _dressUpObjUIs[index].sceneObject == null)
  191. {
  192. return false;
  193. }
  194. Transform model = _dressUpObjUIs[index].sceneObject.transform.Find("Role");
  195. modelInfos[index].cubismModels = model.GetComponentsInChildren<CubismRenderController>();
  196. modelInfos[index].spriteRenderers = new List<SpriteRenderer>(model.GetComponentsInChildren<SpriteRenderer>());
  197. return modelInfos[index].cubismModels.Length > 0;
  198. }
  199. private void ShowModel()
  200. {
  201. ChangeModelAlphaAnim(_index, 0, 1);
  202. Transform obj = _dressUpObjUIs[_index].sceneObject.transform;
  203. if(obj != null)
  204. {
  205. obj.gameObject.SetActive(true);
  206. }
  207. }
  208. private void HideModel()
  209. {
  210. InitModel(_index);
  211. ChangeModelAlphaAnim(_index, 1, 0);
  212. Transform obj = _dressUpObjUIs[_index].sceneObject.transform;
  213. if(obj != null)
  214. {
  215. obj.gameObject.SetActive(false);
  216. }
  217. }
  218. private void ChangeComResLayer()
  219. {
  220. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  221. int index = _comModel.target.GetChildIndex(_comModel.m_comModelRes.target);
  222. int index2 = _comModel.target.GetChildIndex(_comModel.m_comModelRes2.target);
  223. _comModel.target.SetChildIndex(_comModel.m_comModelRes.target, index2);
  224. _comModel.target.SetChildIndex(_comModel.m_comModelRes2.target, index);
  225. UI_ComModel.ProxyEnd();
  226. }
  227. private void ChangeModelAlpha(int index, float value)
  228. {
  229. if (modelInfos[index] == null || modelInfos[index].cubismModels == null)
  230. {
  231. return;
  232. }
  233. for (int j = 0; j < modelInfos[index].cubismModels.Length; j++)
  234. {
  235. modelInfos[index].cubismModels[j].Opacity = value;
  236. }
  237. for (int i = 0; i < modelInfos[index].spriteRenderers.Count; i++)
  238. {
  239. if (modelInfos[index].spriteRenderers[i] == null)
  240. {
  241. modelInfos[index].spriteRenderers.RemoveAt(i);
  242. continue;
  243. }
  244. Color color = modelInfos[index].spriteRenderers[i].color;
  245. color.a = value;
  246. modelInfos[index].spriteRenderers[i].color = color;
  247. }
  248. }
  249. private void ChangeModelAlphaAnim(int index, float start, float end, Action action = null)
  250. {
  251. Ease ease = Ease.Linear;
  252. DOTween.To(() => start, (value) =>
  253. {
  254. ChangeModelAlpha(index, value);
  255. }, end, 0.3f).SetEase(ease).OnComplete(() =>
  256. {
  257. action?.Invoke();
  258. }).SetAutoKill().SetId("fade");
  259. }
  260. private void InitDressUpObj()
  261. {
  262. modelInfos[0] = null;
  263. modelInfos[1] = null;
  264. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  265. _dressUpObjUI.UpdateWrapper(_comModel.m_comModelRes.m_holder);
  266. _dressUpObjUI2.UpdateWrapper(_comModel.m_comModelRes2.m_holder);
  267. UI_ComModel.ProxyEnd();
  268. }
  269. }
  270. }