DressUpUtil.cs 19 KB

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