LuckyBoxController.cs 11 KB

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