DressUpUtil.cs 15 KB

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