DressUpUtil.cs 28 KB

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