DressUpUtil.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. using UnityEngine;
  2. using Live2D.Cubism.Rendering;
  3. using System.IO;
  4. using UnityEngine.Rendering;
  5. using YooAsset;
  6. using System.Collections.Generic;
  7. using System;
  8. using cfg.GfgCfg;
  9. namespace GFGGame
  10. {
  11. //提供各种换装相关的方法
  12. public class DressUpUtil
  13. {
  14. public const string HEAD_DEFAULT_RES_NAME = "head";
  15. public const string BODY_DEFAULT_RES_NAME = "body";
  16. public const string ROLE_OBJ_NAME = "Role";
  17. public const string HEAD_SPRITE_NAME = "Head";
  18. public const string BODY_SPRITE_NAME = "Body";
  19. public const string BODY_ANIMATION_NAME = "Body_a";
  20. public const string BODY_EFFECT_OBJ_NAME = "Body_eff";
  21. public const string FORMAT_SPRITE_NAME = "I_{0}_s{1}";
  22. public const string FORMAT_ANIMATION_NAME = "I_{0}_a{1}";
  23. public const string FORMAT_EFFECT_OBJ_NAME = "I_{0}_e{1}";
  24. public static List<DressUpLayerOperation> AddItemAsync(int itemID, GameObject sceneObj,
  25. bool needSetMask = false, bool showAni = true, GameObject parentObj = null, int resLayer = 0)
  26. {
  27. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemID);
  28. if (itemCfg == null) return null;
  29. if (parentObj == null)
  30. {
  31. if (itemCfg.SubType == ConstDressUpItemType.BEI_JING ||
  32. DressUpMenuItemCfg1Array.Instance.CheckIsSceneTypeBySubType(itemCfg.SubType))
  33. {
  34. parentObj = sceneObj;
  35. }
  36. else
  37. {
  38. //角色
  39. Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
  40. parentObj = role.gameObject;
  41. }
  42. }
  43. showAni = showAni || itemCfg.SubType == ConstDressUpItemType.ZHUANG_RONG;
  44. List<DressUpLayerOperation> handlers = new List<DressUpLayerOperation>();
  45. DressUpLayerOperation handler;
  46. if (resLayer > 0)
  47. {
  48. string layerName = "";
  49. switch (resLayer)
  50. {
  51. case 1:
  52. layerName = itemCfg.ResLayer1;
  53. break;
  54. case 2:
  55. layerName = itemCfg.ResLayer2;
  56. break;
  57. case 3:
  58. layerName = itemCfg.ResLayer3;
  59. break;
  60. }
  61. if (!string.IsNullOrEmpty(layerName))
  62. {
  63. handler = UpdateLayerResAsync(itemCfg, parentObj, resLayer, needSetMask, showAni);
  64. if (handler != null) handlers.Add(handler);
  65. }
  66. }
  67. else
  68. {
  69. //普通层
  70. if (!string.IsNullOrEmpty(itemCfg.ResLayer1))
  71. {
  72. handler = UpdateLayerResAsync(itemCfg, parentObj, 1, needSetMask, showAni);
  73. if (handler != null) handlers.Add(handler);
  74. }
  75. //第二层
  76. if (!string.IsNullOrEmpty(itemCfg.ResLayer2))
  77. {
  78. handler = UpdateLayerResAsync(itemCfg, parentObj, 2, needSetMask, showAni);
  79. if (handler != null) handlers.Add(handler);
  80. }
  81. //第三层
  82. if (!string.IsNullOrEmpty(itemCfg.ResLayer3))
  83. {
  84. handler = UpdateLayerResAsync(itemCfg, parentObj, 3, needSetMask, showAni);
  85. if (handler != null) handlers.Add(handler);
  86. }
  87. }
  88. if (handlers.Count > 0)
  89. {
  90. return handlers;
  91. }
  92. return null;
  93. }
  94. public static string GetSpriteName(ItemCfg itemCfg, int layer)
  95. {
  96. if (itemCfg.SubType == ConstDressUpItemType.BEI_JING)
  97. {
  98. return "Bg";
  99. }
  100. return string.Format(FORMAT_SPRITE_NAME, itemCfg.Id, layer);
  101. }
  102. public static void RemoveItem(int itemID, GameObject parentObj)
  103. {
  104. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemID);
  105. if (itemCfg == null || parentObj == null) return;
  106. string spritObjName;
  107. string aniObjName;
  108. //默认层
  109. if (!string.IsNullOrEmpty(itemCfg.ResLayer1))
  110. {
  111. spritObjName = GetSpriteName(itemCfg, 1);
  112. TryRemoveSprite(parentObj, spritObjName);
  113. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.Id, 1);
  114. TryRemoveObj(parentObj, aniObjName);
  115. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.Id, 1);
  116. TryRemoveObj(parentObj, aniObjName);
  117. }
  118. //特殊层
  119. if (!string.IsNullOrEmpty(itemCfg.ResLayer2))
  120. {
  121. spritObjName = GetSpriteName(itemCfg, 2);
  122. TryRemoveSprite(parentObj, spritObjName);
  123. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.Id, 2);
  124. TryRemoveObj(parentObj, aniObjName);
  125. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.Id, 2);
  126. TryRemoveObj(parentObj, aniObjName);
  127. }
  128. //第三层
  129. if (!string.IsNullOrEmpty(itemCfg.ResLayer3))
  130. {
  131. spritObjName = GetSpriteName(itemCfg, 3);
  132. TryRemoveSprite(parentObj, spritObjName);
  133. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.Id, 3);
  134. TryRemoveObj(parentObj, aniObjName);
  135. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.Id, 3);
  136. TryRemoveObj(parentObj, aniObjName);
  137. }
  138. }
  139. public static DressUpRemoveOperation RemoveItemAsync(int itemID, GameObject sceneObj,
  140. GameObject parentObj = null)
  141. {
  142. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemID);
  143. if (itemCfg == null) return null;
  144. if (parentObj == null)
  145. {
  146. if (itemCfg.SubType == ConstDressUpItemType.BEI_JING ||
  147. DressUpMenuItemCfg1Array.Instance.CheckIsSceneTypeBySubType(itemCfg.SubType))
  148. {
  149. parentObj = sceneObj;
  150. }
  151. else
  152. {
  153. //角色
  154. Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
  155. parentObj = role.gameObject;
  156. }
  157. }
  158. var operation = new DressUpRemoveOperation(itemID, parentObj);
  159. operation.Begin();
  160. return operation;
  161. }
  162. public static DressUpLayerOperation UpdateHeadAsync(bool show, GameObject sceneObj, bool needSetMask = false,
  163. GameObject parentObj = null)
  164. {
  165. var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
  166. parentObj = parentObj == null ? roleTf.gameObject : parentObj;
  167. string res = null;
  168. string resPath = null;
  169. if (show)
  170. {
  171. res = HEAD_DEFAULT_RES_NAME;
  172. resPath = ResPathUtil.GetDressUpPath(res);
  173. }
  174. DressUpLayerOperation handler = new DressUpLayerOperation(parentObj, needSetMask, false, resPath, null);
  175. handler.InitHead();
  176. handler.Begin();
  177. return handler;
  178. }
  179. public static DressUpLayerOperation UpdateBodyAsync(string actionRes, GameObject sceneObj,
  180. bool needSetMask = false, GameObject parentObj = null, int itemIdLianYiQun = 0)
  181. {
  182. //角色
  183. var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
  184. parentObj = parentObj == null ? roleTf.gameObject : parentObj;
  185. var extPng = "png";
  186. string resPath = null;
  187. string effectResPath = null;
  188. bool showAni = !string.IsNullOrEmpty(actionRes);
  189. string aniResPath = null;
  190. if (showAni)
  191. {
  192. aniResPath = ResPathUtil.GetDressUpAnimationPath(actionRes);
  193. if (!YooAssets.CheckResExist(aniResPath))
  194. {
  195. showAni = false;
  196. }
  197. }
  198. if (showAni)
  199. {
  200. resPath = aniResPath;
  201. //特效
  202. effectResPath = ResPathUtil.GetDressUpEffectPath(actionRes, true);
  203. if (!YooAssets.CheckResExist(effectResPath))
  204. {
  205. effectResPath = null;
  206. }
  207. }
  208. else
  209. {
  210. bool hideBody = false;
  211. if (itemIdLianYiQun > 0)
  212. {
  213. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemIdLianYiQun);
  214. hideBody = itemCfg.HideBody > 0;
  215. }
  216. if (hideBody)
  217. {
  218. resPath = null;
  219. }
  220. else
  221. {
  222. resPath = ResPathUtil.GetDressUpPath(BODY_DEFAULT_RES_NAME, extPng);
  223. }
  224. }
  225. DressUpLayerOperation handler =
  226. new DressUpLayerOperation(parentObj, needSetMask, showAni, resPath, effectResPath);
  227. handler.InitBody();
  228. handler.Begin();
  229. return handler;
  230. }
  231. private static DressUpLayerOperation UpdateLayerResAsync(ItemCfg itemCfg, GameObject parentObj, int layerId,
  232. bool needSetMask, bool showAni = true)
  233. {
  234. string resPath;
  235. string aniResPath = null;
  236. //根据资源存在与否再次检查是否播放动画
  237. if (showAni)
  238. {
  239. aniResPath = ResPathUtil.GetDressUpLayerAnimationResPath(itemCfg, layerId);
  240. if (!YooAssets.CheckResExist(aniResPath))
  241. {
  242. showAni = false;
  243. }
  244. }
  245. if (showAni)
  246. {
  247. resPath = aniResPath;
  248. }
  249. else
  250. {
  251. resPath = ResPathUtil.GetDressUpLayerSpriteResPath(itemCfg, layerId);
  252. }
  253. if (!YooAssets.CheckResExist(resPath))
  254. {
  255. return null;
  256. }
  257. //特效
  258. string effectResPath = ResPathUtil.GetDressUpLayerEffectResPath(itemCfg, layerId, showAni);
  259. if (!YooAssets.CheckResExist(effectResPath))
  260. {
  261. effectResPath = null;
  262. }
  263. DressUpLayerOperation handler =
  264. new DressUpLayerOperation(parentObj, needSetMask, showAni, resPath, effectResPath);
  265. handler.InitLayer(itemCfg, layerId);
  266. handler.Begin();
  267. return handler;
  268. }
  269. public static void AddSpriteObjAsync(
  270. string resPath,
  271. string objName,
  272. GameObject parentObj,
  273. int sortingOrder,
  274. bool needSetMask,
  275. Action<GameObject> onComplete)
  276. {
  277. // 检查资源是否存在
  278. if (!YooAssets.CheckResExist(resPath))
  279. {
  280. onComplete?.Invoke(null);
  281. return;
  282. }
  283. // 检查是否已存在相同对象
  284. var existedObj = GetGameObjExisted(parentObj, objName, resPath);
  285. if (existedObj != null)
  286. {
  287. onComplete?.Invoke(existedObj);
  288. return;
  289. }
  290. // 查找或创建游戏对象
  291. GameObject gameObj = parentObj.transform.Find(objName)?.gameObject;
  292. if (gameObj == null)
  293. {
  294. gameObj = new GameObject(objName);
  295. gameObj.transform.SetParent(parentObj.transform, false);
  296. }
  297. // 获取或添加SpriteRenderer
  298. SpriteRenderer spr = gameObj.GetComponent<SpriteRenderer>();
  299. if (spr == null)
  300. {
  301. spr = gameObj.AddComponent<SpriteRenderer>();
  302. }
  303. // 异步加载精灵位置
  304. LoadSpritePosAsync(resPath, (tx, ty) =>
  305. {
  306. // 设置位置
  307. gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
  308. // 添加精灵
  309. SpriteHelper.AddSpriteTo(spr, resPath);
  310. gameObj.SetActive(true);
  311. spr.sortingOrder = sortingOrder;
  312. // 设置遮罩交互
  313. if (needSetMask)
  314. {
  315. spr.maskInteraction = parentObj.transform.parent != null &&
  316. parentObj.transform.parent.name == "targetRole"
  317. ? SpriteMaskInteraction.VisibleOutsideMask
  318. : SpriteMaskInteraction.VisibleInsideMask;
  319. }
  320. else
  321. {
  322. spr.maskInteraction = SpriteMaskInteraction.None;
  323. }
  324. onComplete?.Invoke(gameObj);
  325. });
  326. }
  327. public static void AddAnimationObjAsync(
  328. string resPath,
  329. string objName,
  330. GameObject parentObj,
  331. int sortingOrder,
  332. Action<GameObject> callback)
  333. {
  334. // 检查已有对象
  335. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  336. if (gameObj != null)
  337. {
  338. callback?.Invoke(gameObj);
  339. return;
  340. }
  341. // 异步加载预制体
  342. PrefabManager.Instance.SpawnAsync(resPath, (loadedObj) =>
  343. {
  344. if (loadedObj == null)
  345. {
  346. callback?.Invoke(null);
  347. return;
  348. }
  349. if (objName == BODY_ANIMATION_NAME)
  350. {
  351. DressUpOffsetInfo dressUpOffsetInfo = loadedObj.GetComponent<DressUpOffsetInfo>();
  352. if (dressUpOffsetInfo == null)
  353. {
  354. dressUpOffsetInfo = loadedObj.AddComponent<DressUpOffsetInfo>();
  355. dressUpOffsetInfo.OffsetPosition = loadedObj.transform.localPosition;
  356. dressUpOffsetInfo.Rotation = loadedObj.transform.localRotation;
  357. }
  358. parentObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
  359. parentObj.transform.rotation = dressUpOffsetInfo.Rotation;
  360. }
  361. // 设置位置和父对象
  362. loadedObj.transform.localPosition = Vector3.zero;
  363. loadedObj.transform.rotation = Quaternion.identity;
  364. loadedObj.name = objName;
  365. loadedObj.transform.SetParent(parentObj.transform, false);
  366. // 设置渲染顺序
  367. var render = loadedObj.GetComponent<CubismRenderController>();
  368. if (render == null && loadedObj.transform.childCount > 0)
  369. {
  370. var childObj = loadedObj.transform.GetChild(0);
  371. if (childObj != null)
  372. {
  373. render = childObj.GetComponent<CubismRenderController>();
  374. }
  375. }
  376. if (render != null && render.gameObject.activeSelf)
  377. {
  378. render.SortingOrder = sortingOrder;
  379. }
  380. SetRenderersOrder(loadedObj, sortingOrder);
  381. callback?.Invoke(loadedObj);
  382. });
  383. }
  384. // public static GameObject AddAnimationObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
  385. // {
  386. // var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  387. // if (gameObj != null)
  388. // {
  389. // return gameObj;
  390. // }
  391. //
  392. // gameObj = PrefabManager.Instance.SpawnSync(resPath);
  393. // if (gameObj == null)
  394. // {
  395. // return null;
  396. // }
  397. //
  398. // if (objName == BODY_ANIMATION_NAME)
  399. // {
  400. // //这里有个需求是一些特殊动作需要自定义位置和角度,
  401. // //实现方法就是美术在动作预制体上设置这个位置,
  402. // //然后程序记录下这个位置,把他设置到role节点上,并把动作节点的位置重置成默认值。
  403. // DressUpOffsetInfo dressUpOffsetInfo = gameObj.GetComponent<DressUpOffsetInfo>();
  404. // if (dressUpOffsetInfo == null)
  405. // {
  406. // dressUpOffsetInfo = gameObj.AddComponent<DressUpOffsetInfo>();
  407. // dressUpOffsetInfo.OffsetPosition = gameObj.transform.localPosition;
  408. // dressUpOffsetInfo.Rotation = gameObj.transform.localRotation;
  409. // }
  410. //
  411. // //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  412. // parentObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
  413. // parentObj.transform.rotation = dressUpOffsetInfo.Rotation;
  414. // //.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
  415. // }
  416. //
  417. // gameObj.transform.localPosition = Vector3.zero;
  418. // gameObj.transform.rotation = Quaternion.identity;
  419. // gameObj.name = objName;
  420. // gameObj.transform.SetParent(parentObj.transform, false);
  421. // var render = gameObj.GetComponent<CubismRenderController>();
  422. // if (render == null && gameObj.transform.childCount > 0)
  423. // {
  424. // var childObj = gameObj.transform.GetChild(0);
  425. // if (childObj != null)
  426. // {
  427. // render = childObj.GetComponent<CubismRenderController>();
  428. // }
  429. // }
  430. //
  431. // if (render != null && render.gameObject.activeSelf == true)
  432. // {
  433. // render.SortingOrder = sortingOrder;
  434. // }
  435. //
  436. // SetRenderersOrder(gameObj, sortingOrder);
  437. // return gameObj;
  438. // }
  439. public static void TryAddEffectObjAsync(string resPath, string objName, GameObject parentObj, int sortingOrder,
  440. Action<GameObject> callback)
  441. {
  442. // 检查是否已存在相同对象
  443. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  444. if (gameObj != null)
  445. {
  446. callback?.Invoke(gameObj);
  447. return;
  448. }
  449. // 异步加载预制体
  450. PrefabManager.Instance.SpawnAsync(resPath, (loadedObj) =>
  451. {
  452. if (loadedObj == null)
  453. {
  454. callback?.Invoke(null);
  455. return;
  456. }
  457. // 处理DressUpOffsetInfo组件
  458. DressUpOffsetInfo dressUpOffsetInfo = loadedObj.GetComponent<DressUpOffsetInfo>();
  459. if (dressUpOffsetInfo == null)
  460. {
  461. dressUpOffsetInfo = loadedObj.AddComponent<DressUpOffsetInfo>();
  462. dressUpOffsetInfo.OffsetPosition = loadedObj.transform.localPosition;
  463. dressUpOffsetInfo.Rotation = loadedObj.transform.localRotation;
  464. }
  465. // 设置位置和旋转
  466. loadedObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
  467. loadedObj.transform.rotation = dressUpOffsetInfo.Rotation;
  468. // 设置父对象和名称
  469. loadedObj.transform.SetParent(parentObj.transform, false);
  470. loadedObj.name = objName;
  471. // 处理SortingGroup组件
  472. var sortingGroup = loadedObj.GetComponent<SortingGroup>();
  473. if (sortingGroup != null)
  474. {
  475. GameObject.Destroy(sortingGroup);
  476. }
  477. // 设置渲染层级
  478. SetRenderersOrder(loadedObj, sortingOrder + 1); //特效层默认高一个层级
  479. callback?.Invoke(loadedObj);
  480. });
  481. }
  482. public static bool TryRemoveObj(GameObject parentObj, string objName)
  483. {
  484. if (parentObj == null)
  485. {
  486. return false;
  487. }
  488. Transform transform = parentObj.transform.Find(objName);
  489. if (transform != null)
  490. {
  491. GameObject gameObj = transform.gameObject;
  492. if (gameObj != null)
  493. {
  494. PrefabManager.Instance.Restore(gameObj);
  495. return true;
  496. }
  497. }
  498. return false;
  499. }
  500. public static bool TryRemoveSprite(GameObject parentObj, string objName)
  501. {
  502. if (parentObj == null)
  503. {
  504. return false;
  505. }
  506. Transform transform = parentObj.transform.Find(objName);
  507. if (transform != null)
  508. {
  509. GameObject gameObj = transform.gameObject;
  510. if (gameObj != null)
  511. {
  512. var spr = gameObj.GetComponent<SpriteRenderer>();
  513. if (spr != null)
  514. {
  515. //LogUtil.LogEditor($"TryRemoveSprite {objName}");
  516. SpriteHelper.RemoveSpriteFrom(spr);
  517. gameObj.SetActive(false);
  518. }
  519. return true;
  520. }
  521. }
  522. return false;
  523. }
  524. public static GameObject GetGameObjExisted(GameObject parentObj, string objName, string resPath)
  525. {
  526. if (parentObj == null)
  527. {
  528. return null;
  529. }
  530. Transform transform = parentObj.transform.Find(objName);
  531. if (transform != null)
  532. {
  533. GameObject gameObj = transform.gameObject;
  534. if (gameObj != null)
  535. {
  536. var assetReleaser = gameObj.GetComponent<AssetReleaser>();
  537. if (assetReleaser != null)
  538. {
  539. if (assetReleaser.ResPath == resPath)
  540. {
  541. return gameObj;
  542. }
  543. }
  544. }
  545. }
  546. return null;
  547. }
  548. // public static void LoadSpritePos(string resPath, out float tx, out float ty)
  549. // {
  550. // int index = resPath.LastIndexOf('.');
  551. // resPath = resPath.Substring(0, index) + ".bytes";
  552. // if (YooAssets.CheckResExist(resPath))
  553. // {
  554. // var handle = YooAssets.LoadAssetSync<TextAsset>(resPath);
  555. // TextAsset asset = handle.AssetObject as TextAsset;
  556. // var st = new MemoryStream(asset.bytes);
  557. // var br = new BinaryReader(st);
  558. // tx = br.ReadInt32() / 100f;
  559. // ty = -br.ReadInt32() / 100f;
  560. // handle.Release();
  561. // }
  562. // else
  563. // {
  564. // tx = 0;
  565. // ty = 0;
  566. // }
  567. // }
  568. public static void LoadSpritePosAsync(string resPath, Action<float, float> onComplete)
  569. {
  570. int index = resPath.LastIndexOf('.');
  571. resPath = resPath.Substring(0, index) + ".bytes";
  572. if (YooAssets.CheckResExist(resPath))
  573. {
  574. var handle = YooAssets.LoadAssetAsync<TextAsset>(resPath);
  575. handle.Completed += (operation) =>
  576. {
  577. if (operation.Status == EOperationStatus.Succeed)
  578. {
  579. TextAsset asset = operation.AssetObject as TextAsset;
  580. using (var st = new MemoryStream(asset.bytes))
  581. using (var br = new BinaryReader(st))
  582. {
  583. float tx = br.ReadInt32() / 100f;
  584. float ty = -br.ReadInt32() / 100f;
  585. onComplete?.Invoke(tx, ty);
  586. }
  587. }
  588. else
  589. {
  590. Debug.LogError($"Failed to load position data: {resPath}");
  591. onComplete?.Invoke(0, 0);
  592. }
  593. operation.Release();
  594. };
  595. }
  596. else
  597. {
  598. onComplete?.Invoke(0, 0);
  599. }
  600. }
  601. public static void SetRenderersOrder(GameObject gameObj, int sortingOrder)
  602. {
  603. var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
  604. for (int i = 0; i < meshRenderers.Length; i++)
  605. {
  606. var renderer = meshRenderers[i].GetComponent<Renderer>();
  607. if (renderer != null)
  608. {
  609. renderer.sortingOrder = sortingOrder;
  610. }
  611. }
  612. ParticleSystem[] particles = gameObj.transform.GetComponentsInChildren<ParticleSystem>();
  613. for (int i = 0; i < particles.Length; i++)
  614. {
  615. var renderer = particles[i].GetComponent<Renderer>();
  616. if (renderer != null)
  617. {
  618. renderer.sortingOrder = sortingOrder;
  619. }
  620. }
  621. var spritesRenderers = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
  622. for (int i = 0; i < spritesRenderers.Length; i++)
  623. {
  624. var spriteRender = spritesRenderers[i];
  625. spriteRender.sortingOrder = sortingOrder;
  626. }
  627. }
  628. public static List<int> GetSuitItems(int suitId, bool isAction, int[] excludeType = null,
  629. bool showOptional = true, bool CheckOwn = true, bool isDress = false)
  630. {
  631. SuitCfg suitCfg = CommonDataManager.Tables.TblSuitCfg.GetOrDefault(suitId);
  632. if (suitCfg == null)
  633. {
  634. return null;
  635. }
  636. List<int> items = new List<int>(suitCfg.Parts);
  637. if (showOptional)
  638. {
  639. if (suitCfg.PartsOptional != null && suitCfg.PartsOptional.Count > 0)
  640. {
  641. items.AddRange(suitCfg.PartsOptional);
  642. }
  643. }
  644. int subType = 0;
  645. //找到要穿的散件
  646. List<int> targetItemList = new List<int>();
  647. foreach (int itemId in items)
  648. {
  649. if (!CheckOwn || DressUpMenuItemDataManager.CheckHasItem(itemId) || isDress)
  650. {
  651. subType = ItemDataManager.GetItemSubType(itemId);
  652. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  653. if (!isAction || notInAction)
  654. {
  655. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  656. {
  657. targetItemList.Add(itemId);
  658. }
  659. }
  660. }
  661. }
  662. return targetItemList;
  663. }
  664. }
  665. }