DressUpLayerOperation.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. mainResAdded = gameObj != null;
  281. asyncOperationCount--;
  282. CheckComplete();
  283. });
  284. }
  285. }
  286. // 检查并处理特效资源(改为异步方式)
  287. if (!string.IsNullOrEmpty(effectResPath))
  288. {
  289. asyncOperationCount++;
  290. DressUpUtil.TryAddEffectObjAsync(
  291. effectResPath,
  292. effectObjName,
  293. parentObj,
  294. sortingOrder,
  295. (gameObj) =>
  296. {
  297. effectResAdded = gameObj != null;
  298. asyncOperationCount--;
  299. CheckComplete();
  300. });
  301. }
  302. // 检查所有异步操作是否完成
  303. void CheckComplete()
  304. {
  305. if (asyncOperationCount <= 0)
  306. {
  307. onComplete?.Invoke();
  308. }
  309. }
  310. // 如果没有异步操作,直接调用完成回调
  311. if (asyncOperationCount == 0)
  312. {
  313. onComplete?.Invoke();
  314. }
  315. }
  316. private void UpdateBody(Action onComplete = null)
  317. {
  318. var spritObjName = DressUpUtil.BODY_SPRITE_NAME;
  319. var aniObjName = DressUpUtil.BODY_ANIMATION_NAME;
  320. var effectObjName = DressUpUtil.BODY_EFFECT_OBJ_NAME;
  321. int sortingOrder = 0;
  322. // 清理旧的资源(保持同步)
  323. var removeBodyAni = DressUpUtil.TryRemoveObj(parentObj, aniObjName);
  324. DressUpUtil.TryRemoveObj(parentObj, effectObjName);
  325. DressUpUtil.TryRemoveSprite(parentObj, spritObjName);
  326. // 异步操作计数器
  327. int asyncOperationCount = 0;
  328. bool mainResAdded = false;
  329. bool effectResAdded = false;
  330. // 处理主资源
  331. if (!string.IsNullOrEmpty(this.resPath))
  332. {
  333. if (this.showAni)
  334. {
  335. // 异步加载动画
  336. asyncOperationCount++;
  337. DressUpUtil.AddAnimationObjAsync(
  338. resPath,
  339. aniObjName,
  340. parentObj,
  341. sortingOrder,
  342. (gameObj) =>
  343. {
  344. if (gameObj != null)
  345. {
  346. mainResAdded = true;
  347. }
  348. asyncOperationCount--;
  349. CheckComplete();
  350. });
  351. }
  352. else
  353. {
  354. // 精灵资源改为异步加载
  355. asyncOperationCount++;
  356. DressUpUtil.AddSpriteObjAsync(
  357. this.resPath,
  358. spritObjName,
  359. parentObj,
  360. sortingOrder,
  361. needSetMask,
  362. (gameObj) =>
  363. {
  364. if (gameObj != null)
  365. {
  366. mainResAdded = true;
  367. }
  368. // 如果从动画切换到精灵且移除了动画,重置位置和旋转
  369. if (!this.showAni && removeBodyAni)
  370. {
  371. parentObj.transform.localPosition = Vector3.zero;
  372. parentObj.transform.localRotation = Quaternion.identity;
  373. }
  374. asyncOperationCount--;
  375. CheckComplete();
  376. });
  377. }
  378. }
  379. // 处理特效资源(改为异步方式)
  380. if (!string.IsNullOrEmpty(effectResPath))
  381. {
  382. asyncOperationCount++;
  383. DressUpUtil.TryAddEffectObjAsync(
  384. effectResPath,
  385. effectObjName,
  386. parentObj,
  387. sortingOrder,
  388. (gameObj) =>
  389. {
  390. if (gameObj != null)
  391. {
  392. effectResAdded = true;
  393. }
  394. asyncOperationCount--;
  395. CheckComplete();
  396. });
  397. }
  398. // 检查所有异步操作是否完成
  399. void CheckComplete()
  400. {
  401. if (asyncOperationCount <= 0)
  402. {
  403. // 额外检查:如果是从动画切换到精灵且移除了动画
  404. if (!this.showAni && removeBodyAni && asyncOperationCount == 0)
  405. {
  406. parentObj.transform.localPosition = Vector3.zero;
  407. parentObj.transform.localRotation = Quaternion.identity;
  408. }
  409. onComplete?.Invoke();
  410. }
  411. }
  412. // 如果没有异步操作,直接调用完成回调
  413. if (asyncOperationCount == 0)
  414. {
  415. onComplete?.Invoke();
  416. }
  417. }
  418. private void UpdateHead(Action onComplete = null)
  419. {
  420. LogUtil.LogEditor("update UpdateHead");
  421. var spritObjName = DressUpUtil.HEAD_SPRITE_NAME;
  422. int sortingOrder = 1;
  423. Transform transform_t = parentObj.transform.Find(spritObjName);
  424. if (!string.IsNullOrEmpty(this.resPath))
  425. {
  426. if (transform_t != null)
  427. {
  428. // 如果对象已存在,直接激活并返回
  429. transform_t.gameObject.SetActive(true);
  430. onComplete?.Invoke();
  431. return;
  432. }
  433. // 异步添加精灵对象
  434. DressUpUtil.AddSpriteObjAsync(
  435. resPath,
  436. spritObjName,
  437. parentObj,
  438. sortingOrder,
  439. needSetMask,
  440. (gameObj) =>
  441. {
  442. // 回调中可以添加额外处理逻辑(如有需要)
  443. onComplete?.Invoke();
  444. });
  445. }
  446. else
  447. {
  448. // 资源路径为空时,隐藏现有对象
  449. if (transform_t == null)
  450. {
  451. onComplete?.Invoke();
  452. return;
  453. }
  454. transform_t.gameObject.SetActive(false);
  455. onComplete?.Invoke();
  456. }
  457. }
  458. }
  459. }