DressUpUtil.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. callback?.Invoke(loadedObj);
  383. });
  384. }
  385. // public static GameObject AddAnimationObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
  386. // {
  387. // var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  388. // if (gameObj != null)
  389. // {
  390. // return gameObj;
  391. // }
  392. //
  393. // gameObj = PrefabManager.Instance.SpawnSync(resPath);
  394. // if (gameObj == null)
  395. // {
  396. // return null;
  397. // }
  398. //
  399. // if (objName == BODY_ANIMATION_NAME)
  400. // {
  401. // //这里有个需求是一些特殊动作需要自定义位置和角度,
  402. // //实现方法就是美术在动作预制体上设置这个位置,
  403. // //然后程序记录下这个位置,把他设置到role节点上,并把动作节点的位置重置成默认值。
  404. // DressUpOffsetInfo dressUpOffsetInfo = gameObj.GetComponent<DressUpOffsetInfo>();
  405. // if (dressUpOffsetInfo == null)
  406. // {
  407. // dressUpOffsetInfo = gameObj.AddComponent<DressUpOffsetInfo>();
  408. // dressUpOffsetInfo.OffsetPosition = gameObj.transform.localPosition;
  409. // dressUpOffsetInfo.Rotation = gameObj.transform.localRotation;
  410. // }
  411. //
  412. // //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  413. // parentObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
  414. // parentObj.transform.rotation = dressUpOffsetInfo.Rotation;
  415. // //.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
  416. // }
  417. //
  418. // gameObj.transform.localPosition = Vector3.zero;
  419. // gameObj.transform.rotation = Quaternion.identity;
  420. // gameObj.name = objName;
  421. // gameObj.transform.SetParent(parentObj.transform, false);
  422. // var render = gameObj.GetComponent<CubismRenderController>();
  423. // if (render == null && gameObj.transform.childCount > 0)
  424. // {
  425. // var childObj = gameObj.transform.GetChild(0);
  426. // if (childObj != null)
  427. // {
  428. // render = childObj.GetComponent<CubismRenderController>();
  429. // }
  430. // }
  431. //
  432. // if (render != null && render.gameObject.activeSelf == true)
  433. // {
  434. // render.SortingOrder = sortingOrder;
  435. // }
  436. //
  437. // SetRenderersOrder(gameObj, sortingOrder);
  438. // return gameObj;
  439. // }
  440. public static void TryAddEffectObjAsync(string resPath, string objName, GameObject parentObj, int sortingOrder,
  441. Action<GameObject> callback)
  442. {
  443. // 检查是否已存在相同对象
  444. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  445. if (gameObj != null)
  446. {
  447. callback?.Invoke(gameObj);
  448. return;
  449. }
  450. // 异步加载预制体
  451. PrefabManager.Instance.SpawnAsync(resPath, (loadedObj) =>
  452. {
  453. if (loadedObj == null)
  454. {
  455. callback?.Invoke(null);
  456. return;
  457. }
  458. // 处理DressUpOffsetInfo组件
  459. DressUpOffsetInfo dressUpOffsetInfo = loadedObj.GetComponent<DressUpOffsetInfo>();
  460. if (dressUpOffsetInfo == null)
  461. {
  462. dressUpOffsetInfo = loadedObj.AddComponent<DressUpOffsetInfo>();
  463. dressUpOffsetInfo.OffsetPosition = loadedObj.transform.localPosition;
  464. dressUpOffsetInfo.Rotation = loadedObj.transform.localRotation;
  465. }
  466. // 设置位置和旋转
  467. loadedObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
  468. loadedObj.transform.rotation = dressUpOffsetInfo.Rotation;
  469. // 设置父对象和名称
  470. loadedObj.transform.SetParent(parentObj.transform, false);
  471. loadedObj.name = objName;
  472. // 处理SortingGroup组件
  473. var sortingGroup = loadedObj.GetComponent<SortingGroup>();
  474. if (sortingGroup != null)
  475. {
  476. GameObject.Destroy(sortingGroup);
  477. }
  478. // 设置渲染层级
  479. SetRenderersOrder(loadedObj, sortingOrder + 1); //特效层默认高一个层级
  480. callback?.Invoke(loadedObj);
  481. });
  482. }
  483. public static bool TryRemoveObj(GameObject parentObj, string objName)
  484. {
  485. if (parentObj == null)
  486. {
  487. return false;
  488. }
  489. Transform transform = parentObj.transform.Find(objName);
  490. if (transform != null)
  491. {
  492. GameObject gameObj = transform.gameObject;
  493. if (gameObj != null)
  494. {
  495. PrefabManager.Instance.Restore(gameObj);
  496. return true;
  497. }
  498. }
  499. return false;
  500. }
  501. public static bool TryRemoveSprite(GameObject parentObj, string objName)
  502. {
  503. if (parentObj == null)
  504. {
  505. return false;
  506. }
  507. Transform transform = parentObj.transform.Find(objName);
  508. if (transform != null)
  509. {
  510. GameObject gameObj = transform.gameObject;
  511. if (gameObj != null)
  512. {
  513. var spr = gameObj.GetComponent<SpriteRenderer>();
  514. if (spr != null)
  515. {
  516. //LogUtil.LogEditor($"TryRemoveSprite {objName}");
  517. SpriteHelper.RemoveSpriteFrom(spr);
  518. gameObj.SetActive(false);
  519. }
  520. return true;
  521. }
  522. }
  523. return false;
  524. }
  525. public static GameObject GetGameObjExisted(GameObject parentObj, string objName, string resPath)
  526. {
  527. if (parentObj == null)
  528. {
  529. return null;
  530. }
  531. Transform transform = parentObj.transform.Find(objName);
  532. if (transform != null)
  533. {
  534. GameObject gameObj = transform.gameObject;
  535. if (gameObj != null)
  536. {
  537. var assetReleaser = gameObj.GetComponent<AssetReleaser>();
  538. if (assetReleaser != null)
  539. {
  540. if (assetReleaser.ResPath == resPath)
  541. {
  542. return gameObj;
  543. }
  544. }
  545. }
  546. }
  547. return null;
  548. }
  549. // public static void LoadSpritePos(string resPath, out float tx, out float ty)
  550. // {
  551. // int index = resPath.LastIndexOf('.');
  552. // resPath = resPath.Substring(0, index) + ".bytes";
  553. // if (YooAssets.CheckResExist(resPath))
  554. // {
  555. // var handle = YooAssets.LoadAssetSync<TextAsset>(resPath);
  556. // TextAsset asset = handle.AssetObject as TextAsset;
  557. // var st = new MemoryStream(asset.bytes);
  558. // var br = new BinaryReader(st);
  559. // tx = br.ReadInt32() / 100f;
  560. // ty = -br.ReadInt32() / 100f;
  561. // handle.Release();
  562. // }
  563. // else
  564. // {
  565. // tx = 0;
  566. // ty = 0;
  567. // }
  568. // }
  569. public static void LoadSpritePosAsync(string resPath, Action<float, float> onComplete)
  570. {
  571. int index = resPath.LastIndexOf('.');
  572. resPath = resPath.Substring(0, index) + ".bytes";
  573. if (YooAssetsEx.CheckResExist(resPath))
  574. {
  575. var handle = YooAssets.LoadAssetAsync<TextAsset>(resPath);
  576. handle.Completed += (operation) =>
  577. {
  578. if (operation.Status == EOperationStatus.Succeed)
  579. {
  580. TextAsset asset = operation.AssetObject as TextAsset;
  581. using (var st = new MemoryStream(asset.bytes))
  582. using (var br = new BinaryReader(st))
  583. {
  584. float tx = br.ReadInt32() / 100f;
  585. float ty = -br.ReadInt32() / 100f;
  586. onComplete?.Invoke(tx, ty);
  587. }
  588. }
  589. else
  590. {
  591. Debug.LogError($"Failed to load position data: {resPath}");
  592. onComplete?.Invoke(0, 0);
  593. }
  594. operation.Release();
  595. };
  596. }
  597. else
  598. {
  599. onComplete?.Invoke(0, 0);
  600. }
  601. }
  602. public static void SetRenderersOrder(GameObject gameObj, int sortingOrder)
  603. {
  604. var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
  605. for (int i = 0; i < meshRenderers.Length; i++)
  606. {
  607. var renderer = meshRenderers[i].GetComponent<Renderer>();
  608. if (renderer != null)
  609. {
  610. renderer.sortingOrder = sortingOrder;
  611. }
  612. }
  613. ParticleSystem[] particles = gameObj.transform.GetComponentsInChildren<ParticleSystem>();
  614. for (int i = 0; i < particles.Length; i++)
  615. {
  616. var renderer = particles[i].GetComponent<Renderer>();
  617. if (renderer != null)
  618. {
  619. renderer.sortingOrder = sortingOrder;
  620. }
  621. }
  622. var spritesRenderers = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
  623. for (int i = 0; i < spritesRenderers.Length; i++)
  624. {
  625. var spriteRender = spritesRenderers[i];
  626. spriteRender.sortingOrder = sortingOrder;
  627. }
  628. }
  629. public static List<int> GetSuitItems(int suitId, bool isAction, int[] excludeType = null,
  630. bool showOptional = true, bool CheckOwn = true, bool isDress = false)
  631. {
  632. SuitCfg suitCfg = CommonDataManager.Tables.TblSuitCfg.GetOrDefault(suitId);
  633. if (suitCfg == null)
  634. {
  635. return null;
  636. }
  637. List<int> items = new List<int>(suitCfg.Parts);
  638. if (showOptional)
  639. {
  640. if (suitCfg.PartsOptional != null && suitCfg.PartsOptional.Count > 0)
  641. {
  642. items.AddRange(suitCfg.PartsOptional);
  643. }
  644. }
  645. int subType = 0;
  646. //找到要穿的散件
  647. List<int> targetItemList = new List<int>();
  648. foreach (int itemId in items)
  649. {
  650. if (!CheckOwn || DressUpMenuItemDataManager.CheckHasItem(itemId) || isDress)
  651. {
  652. subType = ItemDataManager.GetItemSubType(itemId);
  653. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  654. if (!isAction || notInAction)
  655. {
  656. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  657. {
  658. targetItemList.Add(itemId);
  659. }
  660. }
  661. }
  662. }
  663. return targetItemList;
  664. }
  665. public static void LoadSpritePos(string resPath, out float tx, out float ty)
  666. {
  667. int index = resPath.LastIndexOf('.');
  668. resPath = resPath.Substring(0, index) + ".bytes";
  669. if (YooAssetsEx.CheckResExist(resPath))
  670. {
  671. var handle = YooAssets.LoadAssetSync<TextAsset>(resPath);
  672. TextAsset asset = handle.AssetObject as TextAsset;
  673. var st = new MemoryStream(asset.bytes);
  674. var br = new BinaryReader(st);
  675. tx = br.ReadInt32() / 100f;
  676. ty = -br.ReadInt32() / 100f;
  677. handle.Release();
  678. }
  679. else
  680. {
  681. tx = 0;
  682. ty = 0;
  683. }
  684. }
  685. }
  686. }