DressUpLayerOperation.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. public string resPath;
  25. public 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(Action action = null)
  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(action);
  122. break;
  123. case EAction.Body:
  124. UpdateBody(action);
  125. break;
  126. case EAction.Head:
  127. UpdateHead(action);
  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. if (downloaderOperation != null)
  156. {
  157. Progress = downloaderOperation.Progress;
  158. if (downloaderOperation.IsDone)
  159. {
  160. if (downloaderOperation.Status == EOperationStatus.Succeed)
  161. {
  162. foreach (var t in locationsLoading)
  163. {
  164. LoadManager.Instance.SetResDownloaded(t);
  165. }
  166. CheckPreDraw();
  167. }
  168. else
  169. {
  170. _steps = EDressUpSteps.Done;
  171. Status = EOperationStatus.Failed;
  172. }
  173. }
  174. }
  175. }
  176. if (_steps == EDressUpSteps.PreDrawing)
  177. {
  178. //LogUtil.LogEditor($"preRendering {preRendering} {resPath} {TimeHelper.ClientNow()}");
  179. if (preRendering <= 0)
  180. {
  181. _steps = EDressUpSteps.Done;
  182. Status = EOperationStatus.Succeed;
  183. }
  184. preRendering--;
  185. }
  186. }
  187. private void CheckLoadRes()
  188. {
  189. List<string> locations = new List<string>();
  190. if (!string.IsNullOrEmpty(resPath) && !LoadManager.Instance.CheckResExsited(resPath))
  191. {
  192. //需加载
  193. locations.Add(this.resPath);
  194. }
  195. if (!string.IsNullOrEmpty(effectResPath) && !LoadManager.Instance.CheckResExsited(effectResPath))
  196. {
  197. //需加载
  198. locations.Add(effectResPath);
  199. }
  200. if (locations.Count == 0)
  201. {
  202. //文件已在本地,不需要下载
  203. CheckPreDraw();
  204. return;
  205. }
  206. locationsLoading = locations.ToArray();
  207. downloaderOperation = YooAssets.CreateBundleDownloader(locationsLoading, 3, 3);
  208. if (downloaderOperation.TotalDownloadCount == 0)
  209. {
  210. //文件已在本地,不需要下载
  211. CheckPreDraw();
  212. return;
  213. }
  214. ViewManager.Show<ModalStatusView>("资源加载中,请耐心等待...");
  215. //下载
  216. _steps = EDressUpSteps.Loading;
  217. downloaderOperation.BeginDownload();
  218. }
  219. private void CheckPreDraw()
  220. {
  221. if (!string.IsNullOrEmpty(resPath))
  222. {
  223. if (showAni)
  224. {
  225. _steps = EDressUpSteps.PreDrawing;
  226. //设置预渲染帧数
  227. preRendering = PRE_RENDER_FRAME;
  228. //预渲染
  229. PrefabManager.Instance.PreDraw(resPath);
  230. //LogUtil.LogEditor($"PreDraw {resPath} {TimeHelper.ClientNow()}");
  231. return;
  232. }
  233. }
  234. _steps = EDressUpSteps.Done;
  235. Status = EOperationStatus.Succeed;
  236. }
  237. private void UpdateLayer(Action onComplete = null)
  238. {
  239. //LogUtil.LogEditor($"add UpdateLayer {itemCfg.id} layerId {layerId}");
  240. int sortingOrder = ItemTypeCfgArray.Instance.GetSortingOrder(itemCfg.SubType, layerId);
  241. // 清理旧的资源
  242. var spritObjName = DressUpUtil.GetSpriteName(itemCfg, layerId);
  243. DressUpUtil.TryRemoveSprite(parentObj, spritObjName);
  244. var aniObjName = string.Format(DressUpUtil.FORMAT_ANIMATION_NAME, itemCfg.Id, layerId);
  245. DressUpUtil.TryRemoveObj(parentObj, aniObjName);
  246. string effectObjName = string.Format(DressUpUtil.FORMAT_EFFECT_OBJ_NAME, itemCfg.Id, layerId);
  247. DressUpUtil.TryRemoveObj(parentObj, effectObjName);
  248. // 异步操作计数器
  249. int asyncOperationCount = 0;
  250. bool mainResAdded = false;
  251. bool effectResAdded = false;
  252. // 检查并处理主资源
  253. if (!string.IsNullOrEmpty(this.resPath))
  254. {
  255. asyncOperationCount++;
  256. if (this.showAni)
  257. {
  258. // 异步加载动画
  259. DressUpUtil.AddAnimationObjAsync(
  260. resPath,
  261. aniObjName,
  262. parentObj,
  263. sortingOrder,
  264. (gameObj) =>
  265. {
  266. mainResAdded = gameObj != null;
  267. asyncOperationCount--;
  268. CheckComplete();
  269. });
  270. }
  271. else
  272. {
  273. // 精灵资源异步加载
  274. string ext = ItemUtil.GetItemResExt(itemCfg.ItemType, itemCfg.SubType);
  275. DressUpUtil.AddSpriteObjAsync(
  276. resPath,
  277. spritObjName,
  278. parentObj,
  279. sortingOrder,
  280. needSetMask,
  281. (gameObj) =>
  282. {
  283. mainResAdded = gameObj != null;
  284. asyncOperationCount--;
  285. CheckComplete();
  286. });
  287. }
  288. }
  289. // 检查并处理特效资源(改为异步方式)
  290. if (!string.IsNullOrEmpty(effectResPath))
  291. {
  292. asyncOperationCount++;
  293. DressUpUtil.TryAddEffectObjAsync(
  294. effectResPath,
  295. effectObjName,
  296. parentObj,
  297. sortingOrder,
  298. (gameObj) =>
  299. {
  300. effectResAdded = gameObj != null;
  301. asyncOperationCount--;
  302. CheckComplete();
  303. });
  304. }
  305. // 检查所有异步操作是否完成
  306. void CheckComplete()
  307. {
  308. if (asyncOperationCount <= 0)
  309. {
  310. onComplete?.Invoke();
  311. }
  312. }
  313. // 如果没有异步操作,直接调用完成回调
  314. if (asyncOperationCount == 0)
  315. {
  316. onComplete?.Invoke();
  317. }
  318. }
  319. private void UpdateBody(Action onComplete = null)
  320. {
  321. var spritObjName = DressUpUtil.BODY_SPRITE_NAME;
  322. var aniObjName = DressUpUtil.BODY_ANIMATION_NAME;
  323. var effectObjName = DressUpUtil.BODY_EFFECT_OBJ_NAME;
  324. int sortingOrder = 0;
  325. // 清理旧的资源(保持同步)
  326. var removeBodyAni = DressUpUtil.TryRemoveObj(parentObj, aniObjName);
  327. DressUpUtil.TryRemoveObj(parentObj, effectObjName);
  328. DressUpUtil.TryRemoveSprite(parentObj, spritObjName);
  329. // 异步操作计数器
  330. int asyncOperationCount = 0;
  331. bool mainResAdded = false;
  332. bool effectResAdded = false;
  333. // 处理主资源
  334. if (!string.IsNullOrEmpty(this.resPath))
  335. {
  336. if (this.showAni)
  337. {
  338. // 异步加载动画
  339. asyncOperationCount++;
  340. DressUpUtil.AddAnimationObjAsync(
  341. resPath,
  342. aniObjName,
  343. parentObj,
  344. 5,
  345. (gameObj) =>
  346. {
  347. if (gameObj != null)
  348. {
  349. mainResAdded = true;
  350. }
  351. asyncOperationCount--;
  352. CheckComplete();
  353. });
  354. }
  355. else
  356. {
  357. // 精灵资源改为异步加载
  358. asyncOperationCount++;
  359. DressUpUtil.AddSpriteObjAsync(
  360. this.resPath,
  361. spritObjName,
  362. parentObj,
  363. sortingOrder,
  364. needSetMask,
  365. (gameObj) =>
  366. {
  367. if (gameObj != null)
  368. {
  369. mainResAdded = true;
  370. }
  371. // 如果从动画切换到精灵且移除了动画,重置位置和旋转
  372. if (!this.showAni && removeBodyAni)
  373. {
  374. parentObj.transform.localPosition = Vector3.zero;
  375. parentObj.transform.localRotation = Quaternion.identity;
  376. }
  377. asyncOperationCount--;
  378. CheckComplete();
  379. });
  380. }
  381. }
  382. // 处理特效资源(改为异步方式)
  383. if (!string.IsNullOrEmpty(effectResPath))
  384. {
  385. asyncOperationCount++;
  386. DressUpUtil.TryAddEffectObjAsync(
  387. effectResPath,
  388. effectObjName,
  389. parentObj,
  390. sortingOrder,
  391. (gameObj) =>
  392. {
  393. if (gameObj != null)
  394. {
  395. effectResAdded = true;
  396. }
  397. asyncOperationCount--;
  398. CheckComplete();
  399. });
  400. }
  401. // 检查所有异步操作是否完成
  402. void CheckComplete()
  403. {
  404. if (asyncOperationCount <= 0)
  405. {
  406. // 额外检查:如果是从动画切换到精灵且移除了动画
  407. if (!this.showAni && removeBodyAni && asyncOperationCount == 0)
  408. {
  409. parentObj.transform.localPosition = Vector3.zero;
  410. parentObj.transform.localRotation = Quaternion.identity;
  411. }
  412. onComplete?.Invoke();
  413. }
  414. }
  415. // 如果没有异步操作,直接调用完成回调
  416. if (asyncOperationCount == 0)
  417. {
  418. onComplete?.Invoke();
  419. }
  420. }
  421. private void UpdateHead(Action onComplete = null)
  422. {
  423. LogUtil.LogEditor("update UpdateHead");
  424. var spritObjName = DressUpUtil.HEAD_SPRITE_NAME;
  425. int sortingOrder = 1;
  426. Transform transform_t = parentObj.transform.Find(spritObjName);
  427. if (!string.IsNullOrEmpty(this.resPath))
  428. {
  429. if (transform_t != null)
  430. {
  431. // 如果对象已存在,直接激活并返回
  432. transform_t.gameObject.SetActive(true);
  433. onComplete?.Invoke();
  434. return;
  435. }
  436. // 异步添加精灵对象
  437. DressUpUtil.AddSpriteObjAsync(
  438. resPath,
  439. spritObjName,
  440. parentObj,
  441. sortingOrder,
  442. needSetMask,
  443. (gameObj) =>
  444. {
  445. // 回调中可以添加额外处理逻辑(如有需要)
  446. onComplete?.Invoke();
  447. });
  448. }
  449. else
  450. {
  451. // 资源路径为空时,隐藏现有对象
  452. if (transform_t == null)
  453. {
  454. onComplete?.Invoke();
  455. return;
  456. }
  457. transform_t.gameObject.SetActive(false);
  458. onComplete?.Invoke();
  459. }
  460. }
  461. }
  462. }