DressUpUtil.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. public 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. //SpriteRenderer spr = null;
  287. //spr = gameObj_t.GetComponent<SpriteRenderer>();
  288. //if(spr != null)
  289. //{
  290. // spr.sprite = null;
  291. //}
  292. // var assetDisposer = gameObj_t.GetComponent<AssetReleaser>();
  293. // if (assetDisposer != null)
  294. // {
  295. // if (!string.IsNullOrEmpty(assetDisposer.resPath))
  296. // {
  297. // GFGAsset.Release(assetDisposer.resPath);
  298. // assetDisposer.resPath = null;
  299. // }
  300. // }
  301. // SpriteRenderer spr = gameObj_t.GetComponent<SpriteRenderer>();
  302. // if (spr != null)
  303. // {
  304. // GameObject.Destroy(spr);
  305. // }
  306. GameObject.DestroyImmediate(gameObj_t);
  307. }
  308. }
  309. }
  310. private static GameObject AddAnimationObj(string res, string objName, GameObject parentObj, int sortingOrder)
  311. {
  312. string resPath = ResPathUtil.GetDressUpAnimationPath(res);
  313. var prefab = GFGAsset.Load<GameObject>(resPath);
  314. var gameObj = GameObject.Instantiate(prefab);
  315. AddAssetReleaser(gameObj, resPath);
  316. gameObj.name = objName;
  317. gameObj.transform.SetParent(parentObj.transform, false);
  318. var render = gameObj.GetComponent<CubismRenderController>();
  319. if (render == null && gameObj.transform.childCount > 0)
  320. {
  321. var childObj = gameObj.transform.GetChild(0);
  322. if (childObj != null)
  323. {
  324. render = childObj.GetComponent<CubismRenderController>();
  325. }
  326. }
  327. if (render != null)
  328. {
  329. render.SortingOrder = sortingOrder;
  330. }
  331. SetParticleSortingOrder(gameObj, sortingOrder);
  332. return gameObj;
  333. }
  334. private static void TryRemoveAnimationObj(GameObject parentObj, string aniObjName)
  335. {
  336. if (parentObj == null)
  337. {
  338. return;
  339. }
  340. Transform transform = parentObj.transform.Find(aniObjName);
  341. if (transform != null)
  342. {
  343. GameObject gameObj = transform.gameObject;
  344. if (gameObj != null)
  345. {
  346. GameObject.DestroyImmediate(gameObj);
  347. }
  348. }
  349. }
  350. public static GameObject CreateAnimationObj(string resPath)
  351. {
  352. // string resPath = ResPathUtil.GetCardAnimationPath(res);
  353. var prefab = GFGAsset.Load<GameObject>(resPath);
  354. if (prefab == null)
  355. {
  356. return null;
  357. }
  358. var gameObj = GameObject.Instantiate(prefab);
  359. AddAssetReleaser(gameObj, resPath);
  360. return gameObj;
  361. }
  362. private static GameObject AddEffectObj(string res, string objName, GameObject parentObj, int sortingOrder)
  363. {
  364. var resPath = ResPathUtil.GetDressUpEffectPath(res);
  365. GameObject effPre = GFGAsset.Load<GameObject>(resPath);
  366. var gameObj = GameObject.Instantiate(effPre);
  367. AddAssetReleaser(gameObj, resPath);
  368. gameObj.transform.SetParent(parentObj.transform);
  369. gameObj.name = objName;
  370. SetParticleSortingOrder(gameObj, sortingOrder);
  371. return gameObj;
  372. }
  373. private static void LoadSpritePos(string res, out float tx, out float ty)
  374. {
  375. string resPath = ResPathUtil.GetDressUpPath(res, "bytes");
  376. if (VEngine.Versions.Contains(resPath))
  377. {
  378. var asset = GFGAsset.Load<TextAsset>(resPath);
  379. if (asset != null)
  380. {
  381. var st = new MemoryStream(asset.bytes);
  382. var br = new BinaryReader(st);
  383. tx = br.ReadInt32() / 100f;
  384. ty = -br.ReadInt32() / 100f;
  385. GFGAsset.Release(resPath);
  386. return;
  387. }
  388. }
  389. tx = 0;
  390. ty = 0;
  391. }
  392. public static void SetParticleSortingOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  393. {
  394. var count = gameObj.transform.childCount;
  395. for (int i = 0; i < count; i++)
  396. {
  397. var tf = gameObj.transform.GetChild(i);
  398. var ps = tf.GetComponent<ParticleSystem>();
  399. if (ps != null)
  400. {
  401. var renderer = ps.GetComponent<Renderer>();
  402. if (renderer != null)
  403. {
  404. if (isAdd)
  405. {
  406. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  407. }
  408. else
  409. {
  410. renderer.sortingOrder = sortingOrder;
  411. }
  412. }
  413. }
  414. }
  415. }
  416. }
  417. }