DressUpUtil.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using UnityEngine;
  2. using Live2D.Cubism.Rendering;
  3. using System.IO;
  4. using FairyGUI;
  5. namespace GFGGame
  6. {
  7. public class DressUpUtil
  8. {
  9. private const string BODY_DEFAULT_RES_NAME = "renmo";
  10. private const string ROLE_OBJ_NAME = "Role";
  11. private const string BODY_SPRITE_NAME = "Body";
  12. private const string BODY_ANIMATION_NAME = "Body_a";
  13. private const string BODY_EFFECT_OBJ_NAME = "Body_eff";
  14. private const string FORMAT_SPRITE_NAME = "T{0}_s{1}";
  15. private const string FORMAT_ANIMATION_NAME = "T{0}_a{1}";
  16. private const string FORMAT_EFFECT_OBJ_NAME = "T{0}_eff";
  17. private const string FORMAT_LAYER_RES_NAME_WITH_T = "{0}_t";
  18. public static void AddItem(int itemID, GameObject sceneObj, bool needSetMask = false, bool showAni = true, GameObject parentObj = null, int resLayer = 0)
  19. {
  20. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  21. if (itemCfg != null)
  22. {
  23. // GameObject parentObj = null;
  24. if (parentObj == null)
  25. {
  26. if (itemCfg.subType == ConstDressUpItemType.BEI_JING)
  27. {
  28. parentObj = sceneObj;
  29. }
  30. else
  31. {
  32. //角色
  33. Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
  34. parentObj = role.gameObject;
  35. }
  36. }
  37. if (resLayer > 0)
  38. {
  39. string layerName = "";
  40. switch (resLayer)
  41. {
  42. case 1:
  43. layerName = itemCfg.resLayer1;
  44. break;
  45. case 2:
  46. layerName = itemCfg.resLayer2;
  47. break;
  48. case 3:
  49. layerName = itemCfg.resLayer3;
  50. break;
  51. }
  52. if (!string.IsNullOrEmpty(layerName))
  53. {
  54. updateLayerRes(itemCfg, parentObj, resLayer, needSetMask, showAni);
  55. }
  56. // updateLayerRes(itemCfg, parentObj, resLayer, resLayer == 2, needSetMask, showAni);
  57. }
  58. else
  59. {
  60. //普通层
  61. if (!string.IsNullOrEmpty(itemCfg.resLayer1))
  62. {
  63. updateLayerRes(itemCfg, parentObj, 1, needSetMask, showAni);
  64. }
  65. //第二层
  66. if (!string.IsNullOrEmpty(itemCfg.resLayer2))
  67. {
  68. updateLayerRes(itemCfg, parentObj, 2, needSetMask, showAni);
  69. }
  70. //第三层
  71. if (!string.IsNullOrEmpty(itemCfg.resLayer3))
  72. {
  73. updateLayerRes(itemCfg, parentObj, 3, needSetMask, showAni);
  74. }
  75. }
  76. //特效
  77. if (itemCfg.effLayer > 0)
  78. {
  79. var objName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType);
  80. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
  81. int sortingOrder = typeCfg.defaultLayer;
  82. if (itemCfg.effLayer == 2)
  83. {
  84. sortingOrder = typeCfg.specialLayer;
  85. }
  86. AddEffectObj(itemCfg.res, objName, parentObj, sortingOrder);
  87. }
  88. }
  89. }
  90. public static void RemoveItem(int itemID, GameObject sceneObj)
  91. {
  92. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  93. if (itemCfg != null)
  94. {
  95. GameObject parentObj = null;
  96. if (itemCfg.subType == (int)ConstDressUpItemType.BEI_JING)
  97. {
  98. parentObj = sceneObj;
  99. }
  100. else
  101. {
  102. //角色
  103. Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
  104. parentObj = role.gameObject;
  105. }
  106. string spritObjName;
  107. string aniObjName;
  108. //默认层
  109. if (!string.IsNullOrEmpty(itemCfg.resLayer1))
  110. {
  111. spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 1);
  112. TryClearSpriteObj(parentObj, spritObjName);
  113. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 1);
  114. TryRemoveAnimationObj(parentObj, aniObjName);
  115. }
  116. //特殊层
  117. if (!string.IsNullOrEmpty(itemCfg.resLayer2))
  118. {
  119. spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 2);
  120. TryClearSpriteObj(parentObj, spritObjName);
  121. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 2);
  122. TryRemoveAnimationObj(parentObj, aniObjName);
  123. }
  124. //第三层
  125. if (!string.IsNullOrEmpty(itemCfg.resLayer3))
  126. {
  127. spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 3);
  128. TryClearSpriteObj(parentObj, spritObjName);
  129. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 3);
  130. TryRemoveAnimationObj(parentObj, aniObjName);
  131. }
  132. //特效
  133. if (itemCfg.effLayer > 0)
  134. {
  135. string effObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType);
  136. var effTf = parentObj.transform.Find(effObjName);
  137. if (effTf != null)
  138. {
  139. GameObject.DestroyImmediate(effTf.gameObject);
  140. }
  141. }
  142. }
  143. }
  144. public static void UpdateBody(string res, GameObject sceneObj, bool isAni = false, string effRes = null, bool needSetMask = false, GameObject parentObj = null)
  145. {
  146. //角色
  147. var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
  148. parentObj = parentObj == null ? roleTf.gameObject : parentObj;
  149. if (res == null)
  150. {
  151. res = BODY_DEFAULT_RES_NAME;
  152. }
  153. //清理旧的
  154. TryClearSpriteObj(parentObj, BODY_SPRITE_NAME);
  155. TryRemoveAnimationObj(parentObj, BODY_ANIMATION_NAME);
  156. if (isAni)
  157. {
  158. AddAnimationObj(res, BODY_ANIMATION_NAME, parentObj, 0);
  159. }
  160. else
  161. {
  162. AddSpriteObj(res, "png", BODY_SPRITE_NAME, parentObj, 0, needSetMask);
  163. }
  164. //特效
  165. var tf = parentObj.transform.Find(BODY_EFFECT_OBJ_NAME);
  166. if (tf != null)
  167. {
  168. GameObject.DestroyImmediate(tf.gameObject);
  169. }
  170. if (!string.IsNullOrEmpty(effRes))
  171. {
  172. AddEffectObj(effRes, BODY_EFFECT_OBJ_NAME, parentObj, 0);
  173. }
  174. }
  175. public static void AddAssetReleaser(GameObject gameObj, string resPath)
  176. {
  177. var assetDisposer = gameObj.AddComponent<AssetReleaser>();
  178. assetDisposer.resPath = resPath;
  179. }
  180. public static void ChangeAssetReleaser(GameObject gameObj, string resPath)
  181. {
  182. var assetDisposer = gameObj.GetComponent<AssetReleaser>();
  183. if (assetDisposer == null)
  184. {
  185. assetDisposer = gameObj.AddComponent<AssetReleaser>();
  186. }
  187. assetDisposer.resPath = resPath;
  188. }
  189. private static void updateLayerRes(ItemCfg itemCfg, GameObject parentObj, int layerId, bool needSetMask, bool showAni = true)
  190. {
  191. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
  192. string res = itemCfg.res;
  193. int sortingOrder = typeCfg.defaultLayer;
  194. switch (layerId)
  195. {
  196. case 1:
  197. res = itemCfg.resLayer1 == "n" ? res : string.Format("{0}_{1}", res, itemCfg.resLayer1);
  198. break;
  199. case 2:
  200. sortingOrder = typeCfg.specialLayer;
  201. res = itemCfg.resLayer2 == "n" ? res : string.Format("{0}_{1}", res, itemCfg.resLayer2);
  202. break;
  203. case 3:
  204. sortingOrder = typeCfg.thirdlLayer;
  205. res = itemCfg.resLayer3 == "n" ? res : string.Format("{0}_{1}", res, itemCfg.resLayer3);
  206. break;
  207. }
  208. //清理旧的
  209. var spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, layerId);
  210. TryClearSpriteObj(parentObj, spritObjName);
  211. var aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, layerId);
  212. TryRemoveAnimationObj(parentObj, aniObjName);
  213. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  214. AddSpriteObj(res, ext, spritObjName, parentObj, sortingOrder, needSetMask);
  215. if (itemCfg.isAni > 0 && showAni)
  216. {
  217. AddAnimationObj(res, aniObjName, parentObj, sortingOrder);
  218. Timers.inst.Add(0.03f, 1, (obj) =>
  219. {
  220. if (parentObj != null && parentObj.transform != null)
  221. {
  222. Transform tf = parentObj.transform.Find(spritObjName);
  223. if (tf != null && tf.gameObject != null && tf.gameObject.activeInHierarchy)
  224. {
  225. var assetDisposer = tf.gameObject.GetComponent<AssetReleaser>();
  226. if (assetDisposer != null)
  227. {
  228. if (!string.IsNullOrEmpty(assetDisposer.resPath))
  229. {
  230. string resPath = ResPathUtil.GetDressUpPath(res, ext);
  231. if (assetDisposer.resPath == resPath)
  232. {
  233. TryClearSpriteObj(parentObj, spritObjName);
  234. }
  235. }
  236. }
  237. }
  238. }
  239. });
  240. }
  241. }
  242. private static GameObject AddSpriteObj(string res, string ext, string objName, GameObject parentObj, int sortingOrder, bool needSetMask)
  243. {
  244. string resPath = ResPathUtil.GetDressUpPath(res, ext);
  245. SpriteRenderer spr = null;
  246. var gameObj = parentObj.transform.Find(objName)?.gameObject;
  247. if (gameObj == null)
  248. {
  249. gameObj = new GameObject(objName);
  250. gameObj.transform.SetParent(parentObj.transform, false);
  251. AddAssetReleaser(gameObj, resPath);
  252. }
  253. spr = gameObj.GetComponent<SpriteRenderer>();
  254. if (spr == null)
  255. {
  256. spr = gameObj.AddComponent<SpriteRenderer>();
  257. }
  258. float tx, ty;
  259. LoadSpritePos(res, out tx, out ty);
  260. gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
  261. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  262. spr.sprite = sp;
  263. spr.sortingOrder = sortingOrder;
  264. if (needSetMask)
  265. {
  266. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  267. }
  268. else
  269. {
  270. spr.maskInteraction = SpriteMaskInteraction.None;
  271. }
  272. return gameObj;
  273. }
  274. private static void TryClearSpriteObj(GameObject parentObj, string spritObjName)
  275. {
  276. if (parentObj == null)
  277. {
  278. return;
  279. }
  280. Transform transform_t = parentObj.transform.Find(spritObjName);
  281. if (transform_t != null)
  282. {
  283. GameObject gameObj_t = transform_t.gameObject;
  284. if (gameObj_t != null)
  285. {
  286. GameObject.DestroyImmediate(gameObj_t);
  287. //SpriteRenderer spr = null;
  288. //spr = gameObj_t.GetComponent<SpriteRenderer>();
  289. //if(spr != null)
  290. //{
  291. // spr.sprite = null;
  292. //}
  293. //var assetDisposer = gameObj_t.GetComponent<AssetReleaser>();
  294. //if(assetDisposer != null)
  295. //{
  296. // if (!string.IsNullOrEmpty(assetDisposer.resPath))
  297. // {
  298. // GFGAsset.Release(assetDisposer.resPath);
  299. // assetDisposer.resPath = null;
  300. // }
  301. //}
  302. }
  303. }
  304. }
  305. private static GameObject AddAnimationObj(string res, string objName, GameObject parentObj, int sortingOrder)
  306. {
  307. string resPath = ResPathUtil.GetDressUpAnimationPath(res);
  308. var prefab = GFGAsset.Load<GameObject>(resPath);
  309. var gameObj = GameObject.Instantiate(prefab);
  310. AddAssetReleaser(gameObj, resPath);
  311. gameObj.name = objName;
  312. gameObj.transform.SetParent(parentObj.transform, false);
  313. var render = gameObj.GetComponent<CubismRenderController>();
  314. if (render == null && gameObj.transform.childCount > 0)
  315. {
  316. var childObj = gameObj.transform.GetChild(0);
  317. if (childObj != null)
  318. {
  319. render = childObj.GetComponent<CubismRenderController>();
  320. }
  321. }
  322. if (render != null)
  323. {
  324. render.SortingOrder = sortingOrder;
  325. }
  326. SetParticleSortingOrder(gameObj, sortingOrder);
  327. return gameObj;
  328. }
  329. private static void TryRemoveAnimationObj(GameObject parentObj, string aniObjName)
  330. {
  331. if (parentObj == null)
  332. {
  333. return;
  334. }
  335. Transform transform = parentObj.transform.Find(aniObjName);
  336. if (transform != null)
  337. {
  338. GameObject gameObj = transform.gameObject;
  339. if (gameObj != null)
  340. {
  341. GameObject.DestroyImmediate(gameObj);
  342. }
  343. }
  344. }
  345. public static GameObject CreateAnimationObj(string resPath)
  346. {
  347. // string resPath = ResPathUtil.GetCardAnimationPath(res);
  348. var prefab = GFGAsset.Load<GameObject>(resPath);
  349. if (prefab == null)
  350. {
  351. return null;
  352. }
  353. var gameObj = GameObject.Instantiate(prefab);
  354. AddAssetReleaser(gameObj, resPath);
  355. return gameObj;
  356. }
  357. private static GameObject AddEffectObj(string res, string objName, GameObject parentObj, int sortingOrder)
  358. {
  359. var resPath = ResPathUtil.GetDressUpEffectPath(res);
  360. GameObject effPre = GFGAsset.Load<GameObject>(resPath);
  361. var gameObj = GameObject.Instantiate(effPre);
  362. AddAssetReleaser(gameObj, resPath);
  363. gameObj.transform.SetParent(parentObj.transform);
  364. gameObj.name = objName;
  365. SetParticleSortingOrder(gameObj, sortingOrder);
  366. return gameObj;
  367. }
  368. private static void LoadSpritePos(string res, out float tx, out float ty)
  369. {
  370. string resPath = ResPathUtil.GetDressUpPath(res, "bytes");
  371. if (VEngine.Versions.Contains(resPath))
  372. {
  373. var asset = GFGAsset.Load<TextAsset>(resPath);
  374. if (asset != null)
  375. {
  376. var st = new MemoryStream(asset.bytes);
  377. var br = new BinaryReader(st);
  378. tx = br.ReadInt32() / 100f;
  379. ty = -br.ReadInt32() / 100f;
  380. GFGAsset.Release(resPath);
  381. return;
  382. }
  383. }
  384. tx = 0;
  385. ty = 0;
  386. }
  387. public static void SetParticleSortingOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  388. {
  389. var count = gameObj.transform.childCount;
  390. for (int i = 0; i < count; i++)
  391. {
  392. var tf = gameObj.transform.GetChild(i);
  393. var ps = tf.GetComponent<ParticleSystem>();
  394. if (ps != null)
  395. {
  396. var renderer = ps.GetComponent<Renderer>();
  397. if (renderer != null)
  398. {
  399. if (isAdd)
  400. {
  401. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  402. }
  403. else
  404. {
  405. renderer.sortingOrder = sortingOrder;
  406. }
  407. }
  408. }
  409. }
  410. }
  411. }
  412. }