DressUpLayerOperation.cs 11 KB

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