DressUpLayerOperation.cs 11 KB

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