DressUpUtil.cs 29 KB

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