DressUpLayerOperation.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using ET;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using YooAsset;
  5. namespace GFGGame
  6. {
  7. public class DressUpLayerOperation : DressUpOperationBase
  8. {
  9. internal enum EAction
  10. {
  11. Layer,
  12. Body,
  13. Head
  14. }
  15. public const int PRE_RENDER_FRAME = 1;
  16. internal ItemCfg itemCfg;
  17. internal GameObject parentObj;
  18. private int layerId;
  19. private bool needSetMask;
  20. private bool showAni;
  21. private string resPath;
  22. private string effectResPath;
  23. private int preRendering;
  24. private ResourceDownloaderOperation downloaderOperation;
  25. internal EAction actionType;
  26. private string[] locationsLoading;
  27. public DressUpLayerOperation(GameObject parentObj, bool needSetMask, bool showAni, string resPath, string effectResPath)
  28. {
  29. this.parentObj = parentObj;
  30. this.needSetMask = needSetMask;
  31. this.resPath = resPath;
  32. this.effectResPath = effectResPath;
  33. this.showAni = showAni;
  34. preRendering = 0;
  35. }
  36. public void InitLayer(ItemCfg itemCfg, int layerId)
  37. {
  38. Debug.Log($"add InitLayer {itemCfg.id} layerId {layerId}");
  39. this.itemCfg = itemCfg;
  40. this.layerId = layerId;
  41. actionType = EAction.Layer;
  42. }
  43. public void InitBody()
  44. {
  45. Debug.Log("update InitBody");
  46. actionType = EAction.Body;
  47. }
  48. public void InitHead()
  49. {
  50. Debug.Log("update InitHead");
  51. actionType = EAction.Head;
  52. }
  53. internal override bool CheckRepeated(DressUpOperationBase t)
  54. {
  55. DressUpLayerOperation layerOperation = t as DressUpLayerOperation;
  56. if (layerOperation != null && layerOperation.actionType == this.actionType)
  57. {
  58. if(actionType == EAction.Layer)
  59. {
  60. return (layerOperation.parentObj == this.parentObj
  61. && layerOperation.itemCfg == this.itemCfg
  62. && layerOperation.layerId == this.layerId);
  63. }
  64. else
  65. {
  66. return true;
  67. }
  68. }
  69. if(this.actionType == EAction.Layer)
  70. {
  71. DressUpRemoveOperation removeOperation = t as DressUpRemoveOperation;
  72. if(removeOperation != null && this.itemCfg != null)
  73. {
  74. if(removeOperation.itemID == this.itemCfg.id)
  75. {
  76. if(removeOperation.parentObj == this.parentObj)
  77. {
  78. return true;
  79. }
  80. }
  81. }
  82. }
  83. return false;
  84. }
  85. /// <summary>
  86. /// 取消下载
  87. /// </summary>
  88. internal override void Cancel()
  89. {
  90. if (_steps != EDressUpSteps.Done)
  91. {
  92. if (downloaderOperation != null)
  93. {
  94. downloaderOperation.CancelDownload();
  95. }
  96. _steps = EDressUpSteps.Done;
  97. }
  98. Status = EOperationStatus.Failed;
  99. Error = "User cancel.";
  100. }
  101. internal override void UpdateView()
  102. {
  103. if (parentObj == null)
  104. {
  105. this.Release();
  106. return;
  107. }
  108. ViewManager.Hide<ModalStatusView>();
  109. switch (actionType)
  110. {
  111. case EAction.Layer:
  112. UpdateLayer();
  113. break;
  114. case EAction.Body:
  115. UpdateBody();
  116. break;
  117. case EAction.Head:
  118. UpdateHead();
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. internal override void Release()
  125. {
  126. downloaderOperation = null;
  127. this.itemCfg = null;
  128. this.parentObj = null;
  129. locationsLoading = null;
  130. }
  131. internal override void Start()
  132. {
  133. _steps = EDressUpSteps.Check;
  134. Update();
  135. }
  136. internal override void Update()
  137. {
  138. if (_steps == EDressUpSteps.None || _steps == EDressUpSteps.Done)
  139. return;
  140. if (_steps == EDressUpSteps.Check)
  141. {
  142. CheckLoadRes();
  143. }
  144. if(_steps == EDressUpSteps.Loading)
  145. {
  146. Progress = downloaderOperation.Progress;
  147. if(downloaderOperation.IsDone)
  148. {
  149. if (downloaderOperation.Status == EOperationStatus.Succeed)
  150. {
  151. foreach (var t in locationsLoading)
  152. {
  153. LoadManager.Instance.SetResDownloaded(t);
  154. }
  155. CheckPreDraw();
  156. }
  157. else
  158. {
  159. _steps = EDressUpSteps.Done;
  160. Status = EOperationStatus.Failed;
  161. }
  162. }
  163. }
  164. if(_steps == EDressUpSteps.PreDrawing)
  165. {
  166. //Debug.Log($"preRendering {preRendering} {resPath} {TimeHelper.ClientNow()}");
  167. if(preRendering <= 0)
  168. {
  169. _steps = EDressUpSteps.Done;
  170. Status = EOperationStatus.Succeed;
  171. }
  172. preRendering--;
  173. }
  174. }
  175. private void CheckLoadRes()
  176. {
  177. List<string> locations = new List<string>();
  178. if(!string.IsNullOrEmpty(resPath) && !LoadManager.Instance.CheckResExsited(resPath))
  179. {
  180. //需加载
  181. locations.Add(this.resPath);
  182. }
  183. if(!string.IsNullOrEmpty(effectResPath) && !LoadManager.Instance.CheckResExsited(effectResPath))
  184. {
  185. //需加载
  186. locations.Add(effectResPath);
  187. }
  188. if(locations.Count == 0)
  189. {
  190. _steps = EDressUpSteps.Done;
  191. Status = EOperationStatus.Succeed;
  192. return;
  193. }
  194. locationsLoading = locations.ToArray();
  195. downloaderOperation = YooAssets.CreateBundleDownloader(locationsLoading, 3, 3);
  196. if(downloaderOperation.TotalDownloadCount == 0)
  197. {
  198. //文件已在本地,不需要下载
  199. CheckPreDraw();
  200. return;
  201. }
  202. ViewManager.Show<ModalStatusView>("加载中...");
  203. //下载
  204. _steps = EDressUpSteps.Loading;
  205. downloaderOperation.BeginDownload();
  206. }
  207. private void CheckPreDraw()
  208. {
  209. if(!string.IsNullOrEmpty(resPath))
  210. {
  211. if (showAni)
  212. {
  213. _steps = EDressUpSteps.PreDrawing;
  214. //设置预渲染帧数
  215. preRendering = PRE_RENDER_FRAME;
  216. //预渲染
  217. PrefabManager.Instance.PreDraw(resPath);
  218. //Debug.Log($"PreDraw {resPath} {TimeHelper.ClientNow()}");
  219. return;
  220. }
  221. }
  222. _steps = EDressUpSteps.Done;
  223. Status = EOperationStatus.Succeed;
  224. }
  225. private void UpdateLayer()
  226. {
  227. Debug.Log($"add UpdateLayer {itemCfg.id} layerId {layerId}");
  228. //string objName;
  229. //if (showAni)
  230. //{
  231. // objName = string.Format(DressUpUtil.FORMAT_ANIMATION_NAME, itemCfg.subType, layerId);
  232. //}
  233. //else
  234. //{
  235. // objName = string.Format(DressUpUtil.FORMAT_SPRITE_NAME, itemCfg.subType, layerId);
  236. //}
  237. int sortingOrder = ItemTypeCfgArray.Instance.GetSortingOrder(itemCfg.subType, layerId);
  238. //var gameObj = DressUpUtil.GetGameObjExisted(parentObj, objName, resPath);
  239. //if (gameObj != null)
  240. //{
  241. // LogHelper.LogEditor("GetGameObjExisted!");
  242. // DressUpUtil.SetRenderersOrder(gameObj, sortingOrder);
  243. // return;
  244. //}
  245. //清理旧的
  246. var spritObjName = string.Format(DressUpUtil.FORMAT_SPRITE_NAME, itemCfg.subType, layerId);
  247. DressUpUtil.TryRemoveSprite(parentObj, spritObjName);
  248. var aniObjName = string.Format(DressUpUtil.FORMAT_ANIMATION_NAME, itemCfg.subType, layerId);
  249. DressUpUtil.TryRemoveObj(parentObj, aniObjName);
  250. string effectObjName = string.Format(DressUpUtil.FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, layerId);
  251. DressUpUtil.TryRemoveObj(parentObj, effectObjName);
  252. //添加新的
  253. if(!string.IsNullOrEmpty(this.resPath))
  254. {
  255. if (this.showAni)
  256. {
  257. DressUpUtil.AddAnimationObj(resPath, aniObjName, parentObj, sortingOrder);
  258. }
  259. else
  260. {
  261. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  262. DressUpUtil.AddSpriteObj(resPath, spritObjName, parentObj, sortingOrder, needSetMask);
  263. }
  264. }
  265. if (!string.IsNullOrEmpty(effectResPath))
  266. {
  267. DressUpUtil.TryAddEffectObj(effectResPath, effectObjName, parentObj, sortingOrder);
  268. }
  269. }
  270. private void UpdateBody()
  271. {
  272. Debug.Log("update UpdateBody");
  273. var spritObjName = DressUpUtil.BODY_SPRITE_NAME;
  274. var aniObjName = DressUpUtil.BODY_ANIMATION_NAME;
  275. var effectObjName = DressUpUtil.BODY_EFFECT_OBJ_NAME;
  276. //string objName;
  277. //if (this.showAni)
  278. //{
  279. // objName = DressUpUtil.BODY_ANIMATION_NAME;
  280. //}
  281. //else
  282. //{
  283. // objName = DressUpUtil.BODY_SPRITE_NAME;
  284. //}
  285. int sortingOrder = 0;
  286. //var gameObj = DressUpUtil.GetGameObjExisted(parentObj, objName, resPath);
  287. //if (gameObj != null)
  288. //{
  289. // LogHelper.LogEditor("GetGameObjExisted!");
  290. // DressUpUtil.SetRenderersOrder(gameObj, sortingOrder);
  291. // return;
  292. //}
  293. var removeBodyAni = DressUpUtil.TryRemoveObj(parentObj, aniObjName);
  294. DressUpUtil.TryRemoveObj(parentObj, effectObjName);
  295. DressUpUtil.TryRemoveSprite(parentObj, spritObjName);
  296. if (this.showAni)
  297. {
  298. DressUpUtil.AddAnimationObj(this.resPath, aniObjName, parentObj, sortingOrder);
  299. }
  300. else
  301. {
  302. DressUpUtil.AddSpriteObj(this.resPath, spritObjName, parentObj, sortingOrder, needSetMask);
  303. if (removeBodyAni)
  304. {
  305. parentObj.transform.localPosition = Vector3.zero;
  306. parentObj.transform.localRotation = Quaternion.identity;
  307. }
  308. }
  309. if (!string.IsNullOrEmpty(effectResPath))
  310. {
  311. DressUpUtil.TryAddEffectObj(effectResPath, effectObjName, parentObj, sortingOrder);
  312. }
  313. }
  314. private void UpdateHead()
  315. {
  316. Debug.Log("update UpdateHead");
  317. var spritObjName = DressUpUtil.HEAD_SPRITE_NAME;
  318. int sortingOrder = 1;
  319. Transform transform_t = parentObj.transform.Find(spritObjName);
  320. if (!string.IsNullOrEmpty(this.resPath))
  321. {
  322. if (transform_t != null)
  323. {
  324. transform_t.gameObject.SetActive(true);
  325. return;
  326. }
  327. DressUpUtil.AddSpriteObj(resPath, spritObjName, parentObj, sortingOrder, needSetMask);
  328. }
  329. else
  330. {
  331. if (transform_t == null)
  332. {
  333. return;
  334. }
  335. transform_t.gameObject.SetActive(false);
  336. }
  337. }
  338. }
  339. }