DressUpUtil.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. TryRemoveObj(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. TryRemoveObj(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. TryRemoveObj(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. spr.sortingOrder = sortingOrder;
  469. if (needSetMask)
  470. {
  471. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  472. if (parentObj.transform.parent != null && parentObj.transform.parent.name == "targetRole")
  473. {
  474. spr.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;
  475. }
  476. else
  477. {
  478. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  479. }
  480. }
  481. else
  482. {
  483. spr.maskInteraction = SpriteMaskInteraction.None;
  484. }
  485. return gameObj;
  486. }
  487. public static GameObject AddAnimationObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
  488. {
  489. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  490. if (gameObj != null)
  491. {
  492. return gameObj;
  493. }
  494. gameObj = PrefabManager.Instance.SpawnSync(resPath);
  495. if(gameObj == null)
  496. {
  497. return null;
  498. }
  499. if (objName == BODY_ANIMATION_NAME)
  500. {
  501. //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  502. parentObj.transform.SetPositionAndRotation(gameObj.transform.localPosition, gameObj.transform.localRotation);
  503. gameObj.transform.SetPositionAndRotation(new Vector3(), new Quaternion());
  504. }
  505. gameObj.name = objName;
  506. gameObj.transform.SetParent(parentObj.transform, false);
  507. var render = gameObj.GetComponent<CubismRenderController>();
  508. if (render == null && gameObj.transform.childCount > 0)
  509. {
  510. var childObj = gameObj.transform.GetChild(0);
  511. if (childObj != null)
  512. {
  513. render = childObj.GetComponent<CubismRenderController>();
  514. }
  515. }
  516. if (render != null && render.gameObject.activeSelf == true)
  517. {
  518. render.SortingOrder = sortingOrder;
  519. }
  520. SetRenderersOrder(gameObj, sortingOrder);
  521. return gameObj;
  522. }
  523. public static GameObject TryAddEffectObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
  524. {
  525. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  526. if (gameObj != null)
  527. {
  528. return gameObj;
  529. }
  530. gameObj = PrefabManager.Instance.SpawnSync(resPath);
  531. if (gameObj == null)
  532. {
  533. return null;
  534. }
  535. gameObj.transform.SetParent(parentObj.transform, false);
  536. gameObj.name = objName;
  537. var sortingGroup = gameObj.transform.GetComponent<SortingGroup>();
  538. if (sortingGroup != null)
  539. {
  540. GameObject.Destroy(sortingGroup);
  541. }
  542. SetRenderersOrder(gameObj, sortingOrder + 1);//特效层默认高一个层级
  543. return gameObj;
  544. }
  545. public static bool TryRemoveObj(GameObject parentObj, string objName)
  546. {
  547. if (parentObj == null)
  548. {
  549. return false;
  550. }
  551. Transform transform = parentObj.transform.Find(objName);
  552. if (transform != null)
  553. {
  554. GameObject gameObj = transform.gameObject;
  555. if (gameObj != null)
  556. {
  557. PrefabManager.Instance.Restore(gameObj);
  558. return true;
  559. }
  560. }
  561. return false;
  562. }
  563. public static GameObject GetGameObjExisted(GameObject parentObj, string objName, string resPath)
  564. {
  565. if (parentObj == null)
  566. {
  567. return null;
  568. }
  569. Transform transform = parentObj.transform.Find(objName);
  570. if (transform != null)
  571. {
  572. GameObject gameObj = transform.gameObject;
  573. if (gameObj != null)
  574. {
  575. var assetReleaser = gameObj.GetComponent<AssetReleaser>();
  576. if (assetReleaser != null)
  577. {
  578. if (assetReleaser.ResPath == resPath)
  579. {
  580. return gameObj;
  581. }
  582. }
  583. }
  584. }
  585. return null;
  586. }
  587. public static void LoadSpritePos(string resPath, out float tx, out float ty)
  588. {
  589. int index = resPath.LastIndexOf('.');
  590. resPath = resPath.Substring(0, index) + ".bytes";
  591. //resPath = resPath.Replace(".png", ".bytes");
  592. //resPath = resPath.Replace(".jpg", ".bytes");
  593. if (YooAssets.CheckResExist(resPath))
  594. {
  595. //var asset = GFGAsset.Load<TextAsset>(resPath);
  596. var handle = YooAssets.LoadAssetSync<TextAsset>(resPath);
  597. TextAsset asset = handle.AssetObject as TextAsset;
  598. var st = new MemoryStream(asset.bytes);
  599. var br = new BinaryReader(st);
  600. tx = br.ReadInt32() / 100f;
  601. ty = -br.ReadInt32() / 100f;
  602. //GFGAsset.Release(resPath);
  603. handle.Release();
  604. }
  605. else
  606. {
  607. tx = 0;
  608. ty = 0;
  609. }
  610. }
  611. private static void SetRenderersOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  612. {
  613. var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
  614. for (int i = 0; i < meshRenderers.Length; i++)
  615. {
  616. var renderer = meshRenderers[i].GetComponent<Renderer>();
  617. if (renderer != null)
  618. {
  619. if (isAdd)
  620. {
  621. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  622. }
  623. else
  624. {
  625. renderer.sortingOrder = sortingOrder;
  626. }
  627. }
  628. }
  629. ParticleSystem[] particles = gameObj.transform.GetComponentsInChildren<ParticleSystem>();
  630. for (int i = 0; i < particles.Length; i++)
  631. {
  632. var renderer = particles[i].GetComponent<Renderer>();
  633. if (renderer != null)
  634. {
  635. if (isAdd)
  636. {
  637. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  638. }
  639. else
  640. {
  641. renderer.sortingOrder = sortingOrder;
  642. }
  643. }
  644. }
  645. }
  646. //public static void SetSpriteRenderSortingOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  647. //{
  648. // SpriteRenderer[] spriteRenders = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
  649. // for (int i = 0; i < spriteRenders.Length; i++)
  650. // {
  651. // if (isAdd)
  652. // {
  653. // spriteRenders[i].sortingOrder = spriteRenders[i].sortingOrder + sortingOrder;
  654. // }
  655. // else
  656. // {
  657. // spriteRenders[i].sortingOrder = sortingOrder;
  658. // }
  659. // }
  660. //}
  661. }
  662. }