DressUpLayerOperation.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. //LogUtil.LogEditor($"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. //LogUtil.LogEditor("update InitBody");
  46. actionType = EAction.Body;
  47. }
  48. public void InitHead()
  49. {
  50. //LogUtil.LogEditor("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. //LogUtil.LogEditor($"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. //文件已在本地,不需要下载
  191. CheckPreDraw();
  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. //LogUtil.LogEditor($"PreDraw {resPath} {TimeHelper.ClientNow()}");
  219. return;
  220. }
  221. }
  222. _steps = EDressUpSteps.Done;
  223. Status = EOperationStatus.Succeed;
  224. }
  225. private void UpdateLayer()
  226. {
  227. //LogUtil.LogEditor($"add UpdateLayer {itemCfg.id} layerId {layerId}");
  228. int sortingOrder = ItemTypeCfgArray.Instance.GetSortingOrder(itemCfg.subType, layerId);
  229. //清理旧的
  230. var spritObjName = string.Format(DressUpUtil.FORMAT_SPRITE_NAME, itemCfg.subType, layerId);
  231. DressUpUtil.TryRemoveSprite(parentObj, spritObjName);
  232. var aniObjName = string.Format(DressUpUtil.FORMAT_ANIMATION_NAME, itemCfg.subType, layerId);
  233. DressUpUtil.TryRemoveObj(parentObj, aniObjName);
  234. string effectObjName = string.Format(DressUpUtil.FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, layerId);
  235. DressUpUtil.TryRemoveObj(parentObj, effectObjName);
  236. //添加新的
  237. if(!string.IsNullOrEmpty(this.resPath))
  238. {
  239. if (this.showAni)
  240. {
  241. DressUpUtil.AddAnimationObj(resPath, aniObjName, parentObj, sortingOrder);
  242. }
  243. else
  244. {
  245. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  246. DressUpUtil.AddSpriteObj(resPath, spritObjName, parentObj, sortingOrder, needSetMask);
  247. }
  248. }
  249. if (!string.IsNullOrEmpty(effectResPath))
  250. {
  251. DressUpUtil.TryAddEffectObj(effectResPath, effectObjName, parentObj, sortingOrder);
  252. }
  253. }
  254. private void UpdateBody()
  255. {
  256. //LogUtil.LogEditor("update UpdateBody");
  257. var spritObjName = DressUpUtil.BODY_SPRITE_NAME;
  258. var aniObjName = DressUpUtil.BODY_ANIMATION_NAME;
  259. var effectObjName = DressUpUtil.BODY_EFFECT_OBJ_NAME;
  260. int sortingOrder = 0;
  261. var removeBodyAni = DressUpUtil.TryRemoveObj(parentObj, aniObjName);
  262. DressUpUtil.TryRemoveObj(parentObj, effectObjName);
  263. DressUpUtil.TryRemoveSprite(parentObj, spritObjName);
  264. if(!string.IsNullOrEmpty(this.resPath))
  265. {
  266. if (this.showAni)
  267. {
  268. DressUpUtil.AddAnimationObj(this.resPath, aniObjName, parentObj, sortingOrder);
  269. }
  270. else
  271. {
  272. DressUpUtil.AddSpriteObj(this.resPath, spritObjName, parentObj, sortingOrder, needSetMask);
  273. if (removeBodyAni)
  274. {
  275. parentObj.transform.localPosition = Vector3.zero;
  276. parentObj.transform.localRotation = Quaternion.identity;
  277. }
  278. }
  279. }
  280. if (!string.IsNullOrEmpty(effectResPath))
  281. {
  282. DressUpUtil.TryAddEffectObj(effectResPath, effectObjName, parentObj, sortingOrder);
  283. }
  284. }
  285. private void UpdateHead()
  286. {
  287. LogUtil.LogEditor("update UpdateHead");
  288. var spritObjName = DressUpUtil.HEAD_SPRITE_NAME;
  289. int sortingOrder = 1;
  290. Transform transform_t = parentObj.transform.Find(spritObjName);
  291. if (!string.IsNullOrEmpty(this.resPath))
  292. {
  293. if (transform_t != null)
  294. {
  295. transform_t.gameObject.SetActive(true);
  296. return;
  297. }
  298. DressUpUtil.AddSpriteObj(resPath, spritObjName, parentObj, sortingOrder, needSetMask);
  299. }
  300. else
  301. {
  302. if (transform_t == null)
  303. {
  304. return;
  305. }
  306. transform_t.gameObject.SetActive(false);
  307. }
  308. }
  309. }
  310. }