LuckyBoxController.cs 10 KB

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