DressUpUtil.cs 18 KB

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