LuckyBoxController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. namespace GFGGame
  12. {
  13. public class LuckyBoxController
  14. {
  15. private GComponent _com;
  16. // private UI_ComModel _comModel;
  17. private DressUpObjUI _dressUpObjUI;
  18. private DressUpObjUI _dressUpObjUI2;
  19. private LuckyBoxCfg _luckyBoxCfg;
  20. private const int _turnTime = 7;
  21. private int _bgIndex = 0;
  22. private int _modelIndex = 0;
  23. private List<DressUpObjUI> _dressUpObjUIs = new List<DressUpObjUI>(2);
  24. private ModelInfo[] modelInfos = new ModelInfo[2];
  25. private class ModelInfo
  26. {
  27. public CubismRenderController[] cubismModels;
  28. public List<SpriteRenderer> spriteRenderers = new List<SpriteRenderer>();
  29. }
  30. public LuckyBoxController(GComponent comModel)
  31. {
  32. _dressUpObjUI = new DressUpObjUI("SceneDressUp");
  33. _dressUpObjUI2 = new DressUpObjUI("SceneDressUp");
  34. _dressUpObjUIs.Add(_dressUpObjUI);
  35. _dressUpObjUIs.Add(_dressUpObjUI2);
  36. _com = comModel;
  37. }
  38. public void OnShown(int luckyBoxId)
  39. {
  40. _luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(luckyBoxId);
  41. _com.GetChild("loaMask").asLoader.url = ResPathUtil.GetBgImgPath(_luckyBoxCfg.bgResArr[_bgIndex]);
  42. InitDressUpObj();
  43. ChangeRes();
  44. Timers.inst.Remove(UpdateTime);
  45. if (_luckyBoxCfg.resArr.Length > 1 || _luckyBoxCfg.suitIdArr.Length > 1)
  46. {
  47. Timers.inst.Add(_turnTime, 0, UpdateTime);
  48. }
  49. }
  50. private void UpdateTime(object param)
  51. {
  52. _com.GetChild("loaMask").asLoader.url = ResPathUtil.GetBgImgPath(_luckyBoxCfg.bgResArr[_bgIndex]);
  53. _bgIndex = _bgIndex + 1 == _luckyBoxCfg.bgResArr.Length ? 0 : _bgIndex + 1;
  54. if (_luckyBoxCfg.resArr.Length > 0)
  55. {
  56. _modelIndex = _modelIndex + 1 == _luckyBoxCfg.resArr.Length ? 0 : _modelIndex + 1;
  57. }
  58. else
  59. {
  60. _modelIndex = _modelIndex + 1 == _luckyBoxCfg.suitIdArr.Length ? 0 : _modelIndex + 1;
  61. }
  62. if (_luckyBoxCfg.isAni == 0)
  63. {
  64. _com.GetTransition("t0").Play(ChangeRes);
  65. }
  66. else
  67. {
  68. HideModel();
  69. UpdateIndex();
  70. ChangeRes();
  71. }
  72. }
  73. private int _index = 0;
  74. private void ChangeRes()
  75. {
  76. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  77. UI_ComModelRes comRes = _comModel.m_comModelRes;
  78. _comModel.m_loaBg.url = ResPathUtil.GetBgImgPath(_luckyBoxCfg.bgResArr[_bgIndex]);
  79. if (_luckyBoxCfg.resArr.Length > 0)
  80. {
  81. //comRes.m_loaRes.url = string.Format("ui://LuckyBox/{0}", _luckyBoxCfg.resArr[_modelIndex]);
  82. comRes.m_loaRes.url = ResPathUtil.GetBgImgPath(_luckyBoxCfg.resArr[_modelIndex]);
  83. EventAgent.DispatchEvent(ConstMessage.LUCKY_EFFECT_UPDATE, _luckyBoxCfg.resArr[_modelIndex]);
  84. comRes.m_loaTitle.url = ResPathUtil.GetLuckyBoxTitlePath(_luckyBoxCfg.resArr[_modelIndex]);
  85. comRes.m_loaRes.SetPosition(-110, -170, 1);
  86. int direction = _luckyBoxCfg.scaleArr[_modelIndex] >= 0 ? 1 : -1;
  87. float scale = _luckyBoxCfg.scaleArr.Length > 0 ? Math.Abs(_luckyBoxCfg.scaleArr[_modelIndex]) / 10000f : 1;
  88. comRes.m_loaRes.SetScale(direction * scale, scale);
  89. }
  90. else if (_luckyBoxCfg.suitIdArr.Length > 0)
  91. {
  92. int direction = 1;
  93. if (_luckyBoxCfg.scaleArr.Length > 0)
  94. {
  95. direction = _luckyBoxCfg.scaleArr[_modelIndex] >= 0 ? 1 : -1;
  96. }
  97. if (_luckyBoxCfg.isAni == 0)
  98. {
  99. _comModel.m_loaMask.visible = true;
  100. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(_luckyBoxCfg.suitIdArr[_modelIndex]);
  101. comRes.m_loaRes.url = ResPathUtil.GetDressUpPath(suitCfg.aniRes);
  102. float scale = _luckyBoxCfg.scaleArr.Length > 0 ? Math.Abs(_luckyBoxCfg.scaleArr[_modelIndex]) / 10000f : 1;
  103. comRes.m_loaRes.SetScale(direction * scale, scale);
  104. comRes.m_loaRes.SetPosition(_luckyBoxCfg.posArr[_modelIndex][0], _luckyBoxCfg.posArr[_modelIndex][1], 1);
  105. }
  106. else
  107. {
  108. _comModel.m_loaMask.visible = false;
  109. comRes = (_index == 0 ? _comModel.m_comModelRes : _comModel.m_comModelRes2);
  110. int scale = _luckyBoxCfg.scaleArr.Length > 0 ? Math.Abs(_luckyBoxCfg.scaleArr[_modelIndex]) / 10000 : 100;
  111. _comModel.m_loaMask.alpha = 1;
  112. comRes.m_loaRes.url = "";
  113. if(modelInfos[_index] == null)
  114. {
  115. modelInfos[_index] = new ModelInfo();
  116. _dressUpObjUIs[_index].UpdateWrapper(comRes.m_holder);
  117. comRes.m_holder.SetPosition(_luckyBoxCfg.posArr[_modelIndex][0], _luckyBoxCfg.posArr[_modelIndex][1], 1);
  118. _dressUpObjUIs[_index].ResetSceneObj(scale, false, false, null, false);
  119. _dressUpObjUIs[_index].dressUpObj.PutOnSuitCfg(_luckyBoxCfg.suitIdArr[_modelIndex], true, new int[] { ConstDressUpItemType.BEI_JING }, false, false);
  120. Timers.inst.StartCoroutine(DelayShowModel(direction));
  121. }
  122. else
  123. {
  124. ShowModel();
  125. }
  126. }
  127. }
  128. if (_luckyBoxCfg.isAni == 0)
  129. {
  130. _com.GetTransition("t1").Play();
  131. }
  132. UI_ComModel.ProxyEnd();
  133. }
  134. private IEnumerator DelayShowModel(int direction)
  135. {
  136. _dressUpObjUIs[_index].sceneObject.transform.localScale = Vector3.zero;
  137. // 未加载到live2D之前,不做显示
  138. while (!InitModel(_index))
  139. {
  140. yield return new WaitForEndOfFrame();
  141. }
  142. int scale = _luckyBoxCfg.scaleArr.Length > 0 ? Math.Abs(_luckyBoxCfg.scaleArr[_modelIndex]) / 10000 : 100;
  143. _dressUpObjUIs[_index].sceneObject.transform.localScale = new Vector3(direction * scale, scale, scale);
  144. ShowModel();
  145. }
  146. public void OnHide()
  147. {
  148. DOTween.Kill("fade");
  149. _bgIndex = 0;
  150. _modelIndex = 0;
  151. _dressUpObjUI.dressUpObj.TakeOffAll();
  152. ChangeModelAlpha(0, 1);
  153. ChangeModelAlpha(1, 1);
  154. // 隐藏另一个模型
  155. if (_dressUpObjUIs[0].sceneObject != null)
  156. {
  157. _dressUpObjUIs[0].sceneObject.transform.localScale = Vector3.zero;
  158. }
  159. if (_dressUpObjUIs[1].sceneObject != null)
  160. {
  161. _dressUpObjUIs[1].sceneObject.transform.localScale = Vector3.zero;
  162. }
  163. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  164. _comModel.m_comModelRes.m_loaRes.url = "";
  165. _comModel.m_comModelRes.m_loaTitle.url = "";
  166. UI_ComModel.ProxyEnd();
  167. Timers.inst.Remove(UpdateTime);
  168. }
  169. public void Dispose()
  170. {
  171. if (_dressUpObjUI != null)
  172. {
  173. _dressUpObjUI.Dispose();
  174. _dressUpObjUI = null;
  175. }
  176. if (_dressUpObjUI2 != null)
  177. {
  178. _dressUpObjUI2.Dispose();
  179. _dressUpObjUI2 = null;
  180. }
  181. _dressUpObjUIs.Clear();
  182. }
  183. private void UpdateIndex()
  184. {
  185. _index ^= 1;
  186. }
  187. private bool InitModel(int index)
  188. {
  189. if (index >= _dressUpObjUIs.Count || _dressUpObjUIs[index].sceneObject == null)
  190. {
  191. return false;
  192. }
  193. Transform model = _dressUpObjUIs[index].sceneObject.transform.Find("Role");
  194. modelInfos[index].cubismModels = model.GetComponentsInChildren<CubismRenderController>();
  195. modelInfos[index].spriteRenderers = new List<SpriteRenderer>(model.GetComponentsInChildren<SpriteRenderer>());
  196. return modelInfos[index].cubismModels.Length > 0;
  197. }
  198. private void ShowModel()
  199. {
  200. ChangeModelAlphaAnim(_index, 0, 1);
  201. }
  202. private void HideModel()
  203. {
  204. InitModel(_index);
  205. ChangeModelAlphaAnim(_index, 1, 0);
  206. }
  207. private void ChangeComResLayer()
  208. {
  209. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  210. int index = _comModel.target.GetChildIndex(_comModel.m_comModelRes.target);
  211. int index2 = _comModel.target.GetChildIndex(_comModel.m_comModelRes2.target);
  212. _comModel.target.SetChildIndex(_comModel.m_comModelRes.target, index2);
  213. _comModel.target.SetChildIndex(_comModel.m_comModelRes2.target, index);
  214. UI_ComModel.ProxyEnd();
  215. }
  216. private void ChangeModelAlpha(int index, float value)
  217. {
  218. if (modelInfos[index] == null || modelInfos[index].cubismModels == null)
  219. {
  220. return;
  221. }
  222. for (int j = 0; j < modelInfos[index].cubismModels.Length; j++)
  223. {
  224. modelInfos[index].cubismModels[j].Opacity = value;
  225. }
  226. for (int i = 0; i < modelInfos[index].spriteRenderers.Count; i++)
  227. {
  228. if (modelInfos[index].spriteRenderers[i] == null)
  229. {
  230. modelInfos[index].spriteRenderers.RemoveAt(i);
  231. continue;
  232. }
  233. Color color = modelInfos[index].spriteRenderers[i].color;
  234. color.a = value;
  235. modelInfos[index].spriteRenderers[i].color = color;
  236. }
  237. }
  238. private void ChangeModelAlphaAnim(int index, float start, float end, Action action = null)
  239. {
  240. Ease ease = Ease.Linear;
  241. DOTween.To(() => start, (value) =>
  242. {
  243. ChangeModelAlpha(index, value);
  244. }, end, 0.3f).SetEase(ease).OnComplete(() =>
  245. {
  246. action?.Invoke();
  247. }).SetAutoKill().SetId("fade");
  248. }
  249. private void InitDressUpObj()
  250. {
  251. modelInfos[0] = null;
  252. modelInfos[1] = null;
  253. UI_ComModel _comModel = UI_ComModel.Proxy(_com);
  254. _dressUpObjUI.UpdateWrapper(_comModel.m_comModelRes.m_holder);
  255. _dressUpObjUI2.UpdateWrapper(_comModel.m_comModelRes2.m_holder);
  256. UI_ComModel.ProxyEnd();
  257. }
  258. }
  259. }