DressUpLayerOperation.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. using System;
  2. using ET;
  3. using System.Collections.Generic;
  4. using cfg.GfgCfg;
  5. using UnityEngine;
  6. using YooAsset;
  7. namespace GFGGame
  8. {
  9. //换装部件操作器,负责部件换装的资源加载、预渲染等
  10. public class DressUpLayerOperation : DressUpOperationBase
  11. {
  12. internal enum EAction
  13. {
  14. Layer,
  15. Body,
  16. Head
  17. }
  18. public const int PRE_RENDER_FRAME = 1;
  19. internal ItemCfg itemCfg;
  20. internal GameObject parentObj;
  21. private int layerId;
  22. private bool needSetMask;
  23. private bool showAni;
  24. private string resPath;
  25. private string effectResPath;
  26. private int preRendering;
  27. private ResourceDownloaderOperation downloaderOperation;
  28. internal EAction actionType;
  29. private string[] locationsLoading;
  30. public DressUpLayerOperation(GameObject parentObj, bool needSetMask, bool showAni, string resPath,
  31. string effectResPath)
  32. {
  33. this.parentObj = parentObj;
  34. this.needSetMask = needSetMask;
  35. this.resPath = resPath;
  36. this.effectResPath = effectResPath;
  37. this.showAni = showAni;
  38. preRendering = 0;
  39. }
  40. public void InitLayer(ItemCfg itemCfg, int layerId)
  41. {
  42. //LogUtil.LogEditor($"add InitLayer {itemCfg.id} layerId {layerId}");
  43. this.itemCfg = itemCfg;
  44. this.layerId = layerId;
  45. actionType = EAction.Layer;
  46. }
  47. public void InitBody()
  48. {
  49. //LogUtil.LogEditor("update InitBody");
  50. actionType = EAction.Body;
  51. }
  52. public void InitHead()
  53. {
  54. //LogUtil.LogEditor("update InitHead");
  55. actionType = EAction.Head;
  56. }
  57. internal override bool CheckRepeated(DressUpOperationBase t)
  58. {
  59. DressUpLayerOperation layerOperation = t as DressUpLayerOperation;
  60. if (layerOperation != null && layerOperation.actionType == this.actionType)
  61. {
  62. if (actionType == EAction.Layer)
  63. {
  64. return (layerOperation.parentObj == this.parentObj
  65. && layerOperation.itemCfg == this.itemCfg
  66. && layerOperation.layerId == this.layerId);
  67. }
  68. else
  69. {
  70. return true;
  71. }
  72. }
  73. if (this.actionType == EAction.Layer)
  74. {
  75. DressUpRemoveOperation removeOperation = t as DressUpRemoveOperation;
  76. if (removeOperation != null && this.itemCfg != null)
  77. {
  78. if (removeOperation.itemID == this.itemCfg.Id)
  79. {
  80. if (removeOperation.parentObj == this.parentObj)
  81. {
  82. return true;
  83. }
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. /// <summary>
  90. /// 取消下载
  91. /// </summary>
  92. internal override void Cancel()
  93. {
  94. if (_steps != EDressUpSteps.Done)
  95. {
  96. if (downloaderOperation != null)
  97. {
  98. downloaderOperation.CancelDownload();
  99. }
  100. if (_steps == EDressUpSteps.PreDrawing)
  101. {
  102. //取消预渲染
  103. PrefabManager.Instance.CancelPreDraw(resPath);
  104. }
  105. _steps = EDressUpSteps.Done;
  106. }
  107. Status = EOperationStatus.Failed;
  108. Error = "User cancel.";
  109. }
  110. internal override void UpdateView()
  111. {
  112. if (parentObj == null)
  113. {
  114. this.Release();
  115. return;
  116. }
  117. ViewManager.Hide<ModalStatusView>();
  118. switch (actionType)
  119. {
  120. case EAction.Layer:
  121. UpdateLayer();
  122. break;
  123. case EAction.Body:
  124. UpdateBody();
  125. break;
  126. case EAction.Head:
  127. UpdateHead();
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. internal override void Release()
  134. {
  135. downloaderOperation = null;
  136. this.itemCfg = null;
  137. this.parentObj = null;
  138. locationsLoading = null;
  139. }
  140. internal override void Start()
  141. {
  142. _steps = EDressUpSteps.Check;
  143. Update();
  144. }
  145. internal override void Update()
  146. {
  147. if (_steps == EDressUpSteps.None || _steps == EDressUpSteps.Done)
  148. return;
  149. if (_steps == EDressUpSteps.Check)
  150. {
  151. CheckLoadRes();
  152. }
  153. if (_steps == EDressUpSteps.Loading)
  154. {
  155. Progress = downloaderOperation.Progress;
  156. if (downloaderOperation.IsDone)
  157. {
  158. if (downloaderOperation.Status == EOperationStatus.Succeed)
  159. {
  160. foreach (var t in locationsLoading)
  161. {
  162. LoadManager.Instance.SetResDownloaded(t);
  163. }
  164. CheckPreDraw();
  165. }
  166. else
  167. {
  168. _steps = EDressUpSteps.Done;
  169. Status = EOperationStatus.Failed;
  170. }
  171. }
  172. }
  173. if (_steps == EDressUpSteps.PreDrawing)
  174. {
  175. //LogUtil.LogEditor($"preRendering {preRendering} {resPath} {TimeHelper.ClientNow()}");
  176. if (preRendering <= 0)
  177. {
  178. _steps = EDressUpSteps.Done;
  179. Status = EOperationStatus.Succeed;
  180. }
  181. preRendering--;
  182. }
  183. }
  184. private void CheckLoadRes()
  185. {
  186. List<string> locations = new List<string>();
  187. if (!string.IsNullOrEmpty(resPath) && !LoadManager.Instance.CheckResExsited(resPath))
  188. {
  189. //需加载
  190. locations.Add(this.resPath);
  191. }
  192. if (!string.IsNullOrEmpty(effectResPath) && !LoadManager.Instance.CheckResExsited(effectResPath))
  193. {
  194. //需加载
  195. locations.Add(effectResPath);
  196. }
  197. if (locations.Count == 0)
  198. {
  199. //文件已在本地,不需要下载
  200. CheckPreDraw();
  201. return;
  202. }
  203. locationsLoading = locations.ToArray();
  204. downloaderOperation = YooAssets.CreateBundleDownloader(locationsLoading, 3, 3);
  205. if (downloaderOperation.TotalDownloadCount == 0)
  206. {
  207. //文件已在本地,不需要下载
  208. CheckPreDraw();
  209. return;
  210. }
  211. ViewManager.Show<ModalStatusView>("资源加载中,请耐心等待...");
  212. //下载
  213. _steps = EDressUpSteps.Loading;
  214. downloaderOperation.BeginDownload();
  215. }
  216. private void CheckPreDraw()
  217. {
  218. if (!string.IsNullOrEmpty(resPath))
  219. {
  220. if (showAni)
  221. {
  222. _steps = EDressUpSteps.PreDrawing;
  223. //设置预渲染帧数
  224. preRendering = PRE_RENDER_FRAME;
  225. //预渲染
  226. PrefabManager.Instance.PreDraw(resPath);
  227. //LogUtil.LogEditor($"PreDraw {resPath} {TimeHelper.ClientNow()}");
  228. return;
  229. }
  230. }
  231. _steps = EDressUpSteps.Done;
  232. Status = EOperationStatus.Succeed;
  233. }
  234. private void UpdateLayer(Action onComplete = null)
  235. {
  236. //LogUtil.LogEditor($"add UpdateLayer {itemCfg.id} layerId {layerId}");
  237. int sortingOrder = ItemTypeCfgArray.Instance.GetSortingOrder(itemCfg.SubType, layerId);
  238. // 清理旧的
  239. var spritObjName = DressUpUtil.GetSpriteName(itemCfg, layerId);
  240. DressUpUtil.TryRemoveSprite(parentObj, spritObjName);
  241. var aniObjName = string.Format(DressUpUtil.FORMAT_ANIMATION_NAME, itemCfg.Id, layerId);
  242. DressUpUtil.TryRemoveObj(parentObj, aniObjName);
  243. string effectObjName = string.Format(DressUpUtil.FORMAT_EFFECT_OBJ_NAME, itemCfg.Id, layerId);
  244. DressUpUtil.TryRemoveObj(parentObj, effectObjName);
  245. // 异步操作计数器
  246. int asyncOperationCount = 0;
  247. bool mainResAdded = false;
  248. bool effectResAdded = false;
  249. // 检查并处理主资源
  250. if (!string.IsNullOrEmpty(this.resPath))
  251. {
  252. asyncOperationCount++;
  253. if (this.showAni)
  254. {
  255. // 异步加载动画
  256. DressUpUtil.AddAnimationObjAsync(
  257. resPath,
  258. aniObjName,
  259. parentObj,
  260. sortingOrder,
  261. (gameObj) =>
  262. {
  263. mainResAdded = gameObj != null;
  264. asyncOperationCount--;
  265. CheckComplete();
  266. });
  267. }
  268. else
  269. {
  270. // 精灵资源异步加载
  271. string ext = ItemUtil.GetItemResExt(itemCfg.ItemType, itemCfg.SubType);
  272. DressUpUtil.AddSpriteObjAsync(
  273. resPath,
  274. spritObjName,
  275. parentObj,
  276. sortingOrder,
  277. needSetMask,
  278. (gameObj) =>
  279. {
  280. if (gameObj != null)
  281. {
  282. mainResAdded = true;
  283. }
  284. asyncOperationCount--;
  285. CheckComplete();
  286. });
  287. }
  288. }
  289. // 检查并处理特效资源(改为同步方式)
  290. if (!string.IsNullOrEmpty(effectResPath))
  291. {
  292. var effectObj = DressUpUtil.TryAddEffectObj(effectResPath, effectObjName, parentObj, sortingOrder);
  293. effectResAdded = effectObj != null;
  294. }
  295. // 如果没有异步操作,直接调用完成回调
  296. if (asyncOperationCount == 0)
  297. {
  298. onComplete?.Invoke();
  299. }
  300. // 检查所有异步操作是否完成
  301. void CheckComplete()
  302. {
  303. if (asyncOperationCount <= 0)
  304. {
  305. onComplete?.Invoke();
  306. }
  307. }
  308. }
  309. private void UpdateBody(Action onComplete = null)
  310. {
  311. //LogUtil.LogEditor("update UpdateBody");
  312. var spritObjName = DressUpUtil.BODY_SPRITE_NAME;
  313. var aniObjName = DressUpUtil.BODY_ANIMATION_NAME;
  314. var effectObjName = DressUpUtil.BODY_EFFECT_OBJ_NAME;
  315. int sortingOrder = 0;
  316. // 清理旧的资源(保持同步)
  317. var removeBodyAni = DressUpUtil.TryRemoveObj(parentObj, aniObjName);
  318. DressUpUtil.TryRemoveObj(parentObj, effectObjName);
  319. DressUpUtil.TryRemoveSprite(parentObj, spritObjName);
  320. // 异步操作计数器
  321. int asyncOperationCount = 0;
  322. bool mainResAdded = false;
  323. bool effectResAdded = false;
  324. // 处理主资源
  325. if (!string.IsNullOrEmpty(this.resPath))
  326. {
  327. if (this.showAni)
  328. {
  329. // 异步加载动画
  330. DressUpUtil.AddAnimationObjAsync(
  331. resPath,
  332. aniObjName,
  333. parentObj,
  334. sortingOrder,
  335. (gameObj) => { mainResAdded = true; });
  336. }
  337. else
  338. {
  339. // 精灵资源改为异步加载
  340. asyncOperationCount++;
  341. DressUpUtil.AddSpriteObjAsync(
  342. this.resPath,
  343. spritObjName,
  344. parentObj,
  345. sortingOrder,
  346. needSetMask,
  347. (gameObj) =>
  348. {
  349. if (gameObj != null)
  350. {
  351. mainResAdded = true;
  352. }
  353. // 如果从动画切换到精灵且移除了动画,重置位置和旋转
  354. if (!this.showAni && removeBodyAni)
  355. {
  356. parentObj.transform.localPosition = Vector3.zero;
  357. parentObj.transform.localRotation = Quaternion.identity;
  358. }
  359. asyncOperationCount--;
  360. CheckComplete();
  361. });
  362. }
  363. }
  364. // 处理特效资源(假设TryAddEffectObj保持同步)
  365. if (!string.IsNullOrEmpty(effectResPath))
  366. {
  367. DressUpUtil.TryAddEffectObj(effectResPath, effectObjName, parentObj, sortingOrder);
  368. effectResAdded = true;
  369. }
  370. // 如果没有异步操作且需要执行位置重置(动画切换情况)
  371. if (asyncOperationCount == 0 && !this.showAni && removeBodyAni)
  372. {
  373. parentObj.transform.localPosition = Vector3.zero;
  374. parentObj.transform.localRotation = Quaternion.identity;
  375. }
  376. // 检查所有异步操作是否完成
  377. void CheckComplete()
  378. {
  379. if (asyncOperationCount <= 0)
  380. {
  381. onComplete?.Invoke();
  382. }
  383. }
  384. // 如果没有异步操作,直接调用完成回调
  385. if (asyncOperationCount == 0)
  386. {
  387. onComplete?.Invoke();
  388. }
  389. }
  390. private void UpdateHead(Action onComplete = null)
  391. {
  392. LogUtil.LogEditor("update UpdateHead");
  393. var spritObjName = DressUpUtil.HEAD_SPRITE_NAME;
  394. int sortingOrder = 1;
  395. Transform transform_t = parentObj.transform.Find(spritObjName);
  396. if (!string.IsNullOrEmpty(this.resPath))
  397. {
  398. if (transform_t != null)
  399. {
  400. // 如果对象已存在,直接激活并返回
  401. transform_t.gameObject.SetActive(true);
  402. onComplete?.Invoke();
  403. return;
  404. }
  405. // 异步添加精灵对象
  406. DressUpUtil.AddSpriteObjAsync(
  407. resPath,
  408. spritObjName,
  409. parentObj,
  410. sortingOrder,
  411. needSetMask,
  412. (gameObj) =>
  413. {
  414. // 回调中可以添加额外处理逻辑(如有需要)
  415. onComplete?.Invoke();
  416. });
  417. }
  418. else
  419. {
  420. // 资源路径为空时,隐藏现有对象
  421. if (transform_t == null)
  422. {
  423. onComplete?.Invoke();
  424. return;
  425. }
  426. transform_t.gameObject.SetActive(false);
  427. onComplete?.Invoke();
  428. }
  429. }
  430. }
  431. }