LuckyBoxController.cs 10 KB

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