LuckyBoxController.cs 10 KB

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