DressUpUtil.cs 29 KB

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