DressUpUtil.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using UnityEngine;
  2. using System;
  3. using Live2D.Cubism.Rendering;
  4. using System.IO;
  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_OBJ_NAME = "Body";
  12. private const string BODY_EFFECT_OBJ_NAME = "Body_eff";
  13. private const string FORMAT_OBJ_NAME = "T{0}_{1}";
  14. private const string FORMAT_EFFECT_OBJ_NAME = "T{0}_eff";
  15. private const string FORMAT_LAYER_RES_NAME_WITH_T = "{0}_t";
  16. public static void AddItem(int itemID, GameObject sceneObj, bool needSetMask = false, bool showAni = true, GameObject parentObj = null, int layerId = int.MinValue, int resLayer = int.MinValue)
  17. {
  18. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  19. if (itemCfg != null)
  20. {
  21. // GameObject parentObj = null;
  22. if (parentObj == null)
  23. {
  24. if (itemCfg.subType == ConstDressUpItemType.BEI_JING)
  25. {
  26. parentObj = sceneObj;
  27. }
  28. else
  29. {
  30. //角色
  31. Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
  32. parentObj = role.gameObject;
  33. }
  34. }
  35. if (layerId > int.MinValue)
  36. {
  37. updateLayerRes(itemCfg, parentObj, layerId, resLayer == 2, needSetMask, showAni);
  38. }
  39. else
  40. {
  41. //普通层
  42. if (itemCfg.resLayer1 > 0)
  43. {
  44. updateLayerRes(itemCfg, parentObj, 1, itemCfg.resLayer1 == 2, needSetMask, showAni);
  45. }
  46. //特殊层
  47. if (itemCfg.resLayer2 > 0)
  48. {
  49. updateLayerRes(itemCfg, parentObj, 2, itemCfg.resLayer2 == 2, needSetMask, showAni);
  50. }
  51. }
  52. //特效
  53. if (itemCfg.effLayer > 0)
  54. {
  55. var objName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType);
  56. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
  57. int sortingOrder = typeCfg.defaultLayer;
  58. if (itemCfg.effLayer == 2)
  59. {
  60. sortingOrder = typeCfg.specialLayer;
  61. }
  62. AddEffectObj(itemCfg.res, objName, parentObj, sortingOrder);
  63. }
  64. }
  65. }
  66. public static void RemoveItem(int itemID, GameObject sceneObj)
  67. {
  68. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  69. if (itemCfg != null)
  70. {
  71. GameObject parentObj = null;
  72. if (itemCfg.subType == (int)ConstDressUpItemType.BEI_JING)
  73. {
  74. parentObj = sceneObj;
  75. }
  76. else
  77. {
  78. //角色
  79. Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
  80. parentObj = role.gameObject;
  81. }
  82. string objName;
  83. //默认层
  84. if (itemCfg.resLayer1 > 0)
  85. {
  86. objName = string.Format(FORMAT_OBJ_NAME, itemCfg.subType, 1);
  87. Transform transform = parentObj.transform.Find(objName);
  88. if (transform != null)
  89. {
  90. GameObject gameObj = transform.gameObject;
  91. if (gameObj != null)
  92. {
  93. GameObject.DestroyImmediate(gameObj);
  94. }
  95. }
  96. }
  97. //特殊层
  98. if (itemCfg.resLayer2 > 0)
  99. {
  100. objName = string.Format(FORMAT_OBJ_NAME, itemCfg.subType, 2);
  101. Transform transform_t = parentObj.transform.Find(objName);
  102. if (transform_t != null)
  103. {
  104. GameObject gameObj_t = transform_t.gameObject;
  105. if (gameObj_t != null)
  106. {
  107. GameObject.DestroyImmediate(gameObj_t);
  108. }
  109. }
  110. }
  111. //特效
  112. if (itemCfg.effLayer > 0)
  113. {
  114. string effObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType);
  115. var effTf = parentObj.transform.Find(effObjName);
  116. if (effTf != null)
  117. {
  118. GameObject.DestroyImmediate(effTf.gameObject);
  119. }
  120. }
  121. }
  122. }
  123. public static void UpdateBody(string res, GameObject sceneObj, bool isAni = false, string effRes = null, bool needSetMask = false, GameObject parentObj = null)
  124. {
  125. //角色
  126. Transform roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
  127. GameObject roleObj = parentObj == null ? roleTf.gameObject : parentObj;
  128. if (res == null)
  129. {
  130. res = BODY_DEFAULT_RES_NAME;
  131. }
  132. var objName = BODY_OBJ_NAME;
  133. Transform tf = roleObj.transform.Find(objName);
  134. if (tf != null)
  135. {
  136. GameObject.DestroyImmediate(tf.gameObject);
  137. tf = null;
  138. }
  139. if (isAni)
  140. {
  141. AddAnimationObj(res, objName, roleObj, 0);
  142. }
  143. else
  144. {
  145. AddSpriteObj(res, "png", objName, roleObj, 0, needSetMask);
  146. }
  147. //特效
  148. objName = BODY_EFFECT_OBJ_NAME;
  149. tf = roleObj.transform.Find(objName);
  150. if (tf != null)
  151. {
  152. GameObject.DestroyImmediate(tf.gameObject);
  153. }
  154. if (!string.IsNullOrEmpty(effRes))
  155. {
  156. AddEffectObj(effRes, objName, roleObj, 0);
  157. }
  158. }
  159. public static void AddAssetReleaser(GameObject gameObj, string resPath)
  160. {
  161. var assetDisposer = gameObj.AddComponent<AssetReleaser>();
  162. assetDisposer.resPath = resPath;
  163. }
  164. public static void ChangeAssetReleaser(GameObject gameObj, string resPath)
  165. {
  166. var assetDisposer = gameObj.GetComponent<AssetReleaser>();
  167. if (assetDisposer == null)
  168. {
  169. assetDisposer = gameObj.AddComponent<AssetReleaser>();
  170. }
  171. assetDisposer.resPath = resPath;
  172. }
  173. private static void updateLayerRes(ItemCfg itemCfg, GameObject parentObj, int layerId, bool isT, bool needSetMask, bool showAni = true)
  174. {
  175. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
  176. string objName = string.Format(FORMAT_OBJ_NAME, typeCfg.type, layerId);
  177. string res = itemCfg.res;
  178. int sortingOrder = typeCfg.defaultLayer;
  179. if (layerId == 2)
  180. {
  181. sortingOrder = typeCfg.specialLayer;
  182. }
  183. if (isT)
  184. {
  185. res = string.Format(FORMAT_LAYER_RES_NAME_WITH_T, res);
  186. }
  187. Transform tf = parentObj.transform.Find(objName);
  188. if (tf != null)
  189. {
  190. GameObject.DestroyImmediate(tf.gameObject);
  191. tf = null;
  192. }
  193. if (itemCfg.isAni > 0 && showAni)
  194. {
  195. AddAnimationObj(res, objName, parentObj, sortingOrder);
  196. }
  197. else
  198. {
  199. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  200. AddSpriteObj(res, ext, objName, parentObj, sortingOrder, needSetMask);
  201. }
  202. }
  203. private static GameObject AddSpriteObj(string res, string ext, string objName, GameObject parentObj, int sortingOrder, bool needSetMask)
  204. {
  205. string resPath = ResPathUtil.GetDressUpPath(res, ext);
  206. SpriteRenderer spr = null;
  207. var gameObj = new GameObject(objName);
  208. spr = gameObj.AddComponent<SpriteRenderer>();
  209. AddAssetReleaser(gameObj, resPath);
  210. gameObj.transform.SetParent(parentObj.transform, false);
  211. float tx, ty;
  212. LoadSpritePos(res, out tx, out ty);
  213. gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
  214. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  215. spr.sprite = sp;
  216. spr.sortingOrder = sortingOrder;
  217. if (needSetMask)
  218. {
  219. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  220. }
  221. else
  222. {
  223. spr.maskInteraction = SpriteMaskInteraction.None;
  224. }
  225. return gameObj;
  226. }
  227. private static GameObject AddAnimationObj(string res, string objName, GameObject parentObj, int sortingOrder)
  228. {
  229. string resPath = ResPathUtil.GetDressUpAnimationPath(res);
  230. var prefab = GFGAsset.Load<GameObject>(resPath);
  231. var gameObj = GameObject.Instantiate(prefab);
  232. AddAssetReleaser(gameObj, resPath);
  233. gameObj.name = objName;
  234. gameObj.transform.SetParent(parentObj.transform, false);
  235. var render = gameObj.GetComponent<CubismRenderController>();
  236. if (render == null && gameObj.transform.childCount > 0)
  237. {
  238. var childObj = gameObj.transform.GetChild(0);
  239. if (childObj != null)
  240. {
  241. render = childObj.GetComponent<CubismRenderController>();
  242. }
  243. }
  244. if (render != null)
  245. {
  246. render.SortingOrder = sortingOrder;
  247. }
  248. SetParticleSortingOrder(gameObj, sortingOrder);
  249. return gameObj;
  250. }
  251. public static GameObject AddAnimationObj(string resPath)
  252. {
  253. // string resPath = ResPathUtil.GetCardAnimationPath(res);
  254. var prefab = GFGAsset.Load<GameObject>(resPath);
  255. if (prefab == null)
  256. {
  257. return null;
  258. }
  259. var gameObj = GameObject.Instantiate(prefab);
  260. AddAssetReleaser(gameObj, resPath);
  261. return gameObj;
  262. }
  263. private static GameObject AddEffectObj(string res, string objName, GameObject parentObj, int sortingOrder)
  264. {
  265. var resPath = ResPathUtil.GetDressUpEffectPath(res);
  266. GameObject effPre = GFGAsset.Load<GameObject>(resPath);
  267. var gameObj = GameObject.Instantiate(effPre);
  268. AddAssetReleaser(gameObj, resPath);
  269. gameObj.transform.SetParent(parentObj.transform);
  270. gameObj.name = objName;
  271. SetParticleSortingOrder(gameObj, sortingOrder);
  272. return gameObj;
  273. }
  274. public static void LoadSpritePos(string res, out float tx, out float ty)
  275. {
  276. string resPath = ResPathUtil.GetDressUpPath(res, "bytes");
  277. if (VEngine.Versions.Contains(resPath))
  278. {
  279. var asset = GFGAsset.Load<TextAsset>(resPath);
  280. if (asset != null)
  281. {
  282. var st = new MemoryStream(asset.bytes);
  283. var br = new BinaryReader(st);
  284. tx = br.ReadInt32() / 100f;
  285. ty = -br.ReadInt32() / 100f;
  286. GFGAsset.Release(resPath);
  287. return;
  288. }
  289. }
  290. tx = 0;
  291. ty = 0;
  292. }
  293. public static void SetParticleSortingOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  294. {
  295. var count = gameObj.transform.childCount;
  296. for (int i = 0; i < count; i++)
  297. {
  298. var tf = gameObj.transform.GetChild(i);
  299. var ps = tf.GetComponent<ParticleSystem>();
  300. if (ps != null)
  301. {
  302. var renderer = ps.GetComponent<Renderer>();
  303. if (renderer != null)
  304. {
  305. if (isAdd)
  306. {
  307. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  308. }
  309. else
  310. {
  311. renderer.sortingOrder = sortingOrder;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }