DressUpUtil.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using UnityEngine;
  2. using Live2D.Cubism.Rendering;
  3. using System.IO;
  4. using FairyGUI;
  5. using UnityEngine.Rendering;
  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. public 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}_e{1}";
  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 || DressUpMenuItemCfg1Array.Instance.CheckIsSceneTypeBySubType(itemCfg.subType))
  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. showAni = showAni || itemCfg.subType == ConstDressUpItemType.ZHUANG_RONG;
  40. if (resLayer > 0)
  41. {
  42. string layerName = "";
  43. switch (resLayer)
  44. {
  45. case 1:
  46. layerName = itemCfg.resLayer1;
  47. break;
  48. case 2:
  49. layerName = itemCfg.resLayer2;
  50. break;
  51. case 3:
  52. layerName = itemCfg.resLayer3;
  53. break;
  54. }
  55. if (!string.IsNullOrEmpty(layerName))
  56. {
  57. updateLayerRes(itemCfg, parentObj, resLayer, needSetMask, showAni);
  58. }
  59. // updateLayerRes(itemCfg, parentObj, resLayer, resLayer == 2, needSetMask, showAni);
  60. }
  61. else
  62. {
  63. //普通层
  64. if (!string.IsNullOrEmpty(itemCfg.resLayer1))
  65. {
  66. updateLayerRes(itemCfg, parentObj, 1, needSetMask, showAni);
  67. }
  68. //第二层
  69. if (!string.IsNullOrEmpty(itemCfg.resLayer2))
  70. {
  71. updateLayerRes(itemCfg, parentObj, 2, needSetMask, showAni);
  72. }
  73. //第三层
  74. if (!string.IsNullOrEmpty(itemCfg.resLayer3))
  75. {
  76. updateLayerRes(itemCfg, parentObj, 3, needSetMask, showAni);
  77. }
  78. }
  79. }
  80. }
  81. public static void RemoveItem(int itemID, GameObject sceneObj, GameObject parentObj = null)
  82. {
  83. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  84. if (itemCfg != null)
  85. {
  86. if (parentObj == null)
  87. {
  88. if (itemCfg.subType == ConstDressUpItemType.BEI_JING || DressUpMenuItemCfg1Array.Instance.CheckIsSceneTypeBySubType(itemCfg.subType))
  89. {
  90. parentObj = sceneObj;
  91. }
  92. else
  93. {
  94. //角色
  95. Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
  96. parentObj = role.gameObject;
  97. }
  98. }
  99. string spritObjName;
  100. string aniObjName;
  101. //默认层
  102. if (!string.IsNullOrEmpty(itemCfg.resLayer1))
  103. {
  104. spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 1);
  105. TryRemoveObj(parentObj, spritObjName);
  106. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 1);
  107. TryRemoveObj(parentObj, aniObjName);
  108. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, 1);
  109. TryRemoveObj(parentObj, aniObjName);
  110. }
  111. //特殊层
  112. if (!string.IsNullOrEmpty(itemCfg.resLayer2))
  113. {
  114. spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 2);
  115. TryRemoveObj(parentObj, spritObjName);
  116. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 2);
  117. TryRemoveObj(parentObj, aniObjName);
  118. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, 2);
  119. TryRemoveObj(parentObj, aniObjName);
  120. }
  121. //第三层
  122. if (!string.IsNullOrEmpty(itemCfg.resLayer3))
  123. {
  124. spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 3);
  125. TryRemoveObj(parentObj, spritObjName);
  126. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 3);
  127. TryRemoveObj(parentObj, aniObjName);
  128. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, 3);
  129. TryRemoveObj(parentObj, aniObjName);
  130. }
  131. }
  132. }
  133. public static void UpdateHead(bool show, GameObject sceneObj, bool needSetMask = false, GameObject parentObj = null)
  134. {
  135. var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
  136. parentObj = parentObj == null ? roleTf.gameObject : parentObj;
  137. string res = HEAD_DEFAULT_RES_NAME;
  138. Transform transform_t = parentObj.transform.Find(HEAD_SPRITE_NAME);
  139. if (show)
  140. {
  141. if (transform_t != null)
  142. {
  143. transform_t.gameObject.SetActive(true);
  144. return;
  145. }
  146. AddSpriteObj(res, "png", HEAD_SPRITE_NAME, parentObj, 1, needSetMask);
  147. }
  148. else
  149. {
  150. if(transform_t == null)
  151. {
  152. return;
  153. }
  154. transform_t.gameObject.SetActive(false);
  155. }
  156. }
  157. public static void UpdateBody(string actionRes, GameObject sceneObj, bool needSetMask = false, GameObject parentObj = null)
  158. {
  159. //角色
  160. var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
  161. parentObj = parentObj == null ? roleTf.gameObject : parentObj;
  162. var extPng = "png";
  163. if (!string.IsNullOrEmpty(actionRes))
  164. {
  165. string resPath = ResPathUtil.GetDressUpAnimationPath(actionRes);
  166. if (CheckGameObjExisted(parentObj, BODY_ANIMATION_NAME, resPath))
  167. {
  168. return;
  169. }
  170. }
  171. else
  172. {
  173. string resPath = ResPathUtil.GetDressUpPath(BODY_DEFAULT_RES_NAME, extPng);
  174. if (CheckGameObjExisted(parentObj, BODY_SPRITE_NAME, resPath))
  175. {
  176. return;
  177. }
  178. }
  179. var removeBodyAni = TryRemoveObj(parentObj, BODY_ANIMATION_NAME);
  180. TryRemoveObj(parentObj, BODY_EFFECT_OBJ_NAME);
  181. TryRemoveObj(parentObj, BODY_SPRITE_NAME);
  182. if (!string.IsNullOrEmpty(actionRes))
  183. {
  184. AddAnimationObj(actionRes, BODY_ANIMATION_NAME, parentObj, 0);
  185. //特效
  186. TryAddEffectObj(actionRes, BODY_EFFECT_OBJ_NAME, parentObj, 0, false);
  187. }
  188. else
  189. {
  190. AddSpriteObj(BODY_DEFAULT_RES_NAME, extPng, BODY_SPRITE_NAME, parentObj, 0, needSetMask);
  191. if (removeBodyAni)
  192. {
  193. parentObj.transform.SetPositionAndRotation(new Vector3(), new Quaternion());
  194. }
  195. }
  196. }
  197. public static void AddAssetReleaser(GameObject gameObj, string resPath)
  198. {
  199. var assetDisposer = gameObj.AddComponent<AssetReleaser>();
  200. assetDisposer.resPath = resPath;
  201. }
  202. public static void ChangeAssetReleaser(GameObject gameObj, string resPath)
  203. {
  204. var assetDisposer = gameObj.GetComponent<AssetReleaser>();
  205. if (assetDisposer == null)
  206. {
  207. assetDisposer = gameObj.AddComponent<AssetReleaser>();
  208. }
  209. assetDisposer.resPath = resPath;
  210. }
  211. public static string GetDressUpItemLayerRes(ItemCfg itemCfg, int layerId)
  212. {
  213. string res = itemCfg.res;
  214. switch (layerId)
  215. {
  216. case 1:
  217. res = itemCfg.resLayer1 == "n" ? res : string.Format("{0}_{1}", res, itemCfg.resLayer1);
  218. break;
  219. case 2:
  220. res = itemCfg.resLayer2 == "n" ? res : string.Format("{0}_{1}", res, itemCfg.resLayer2);
  221. break;
  222. case 3:
  223. res = itemCfg.resLayer3 == "n" ? res : string.Format("{0}_{1}", res, itemCfg.resLayer3);
  224. break;
  225. }
  226. return res;
  227. }
  228. private static void updateLayerRes(ItemCfg itemCfg, GameObject parentObj, int layerId, bool needSetMask, bool showAni = true)
  229. {
  230. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
  231. string res = GetDressUpItemLayerRes(itemCfg, layerId);
  232. int sortingOrder = typeCfg.defaultLayer;
  233. if(layerId == 2)
  234. {
  235. sortingOrder = typeCfg.specialLayer;
  236. }
  237. else if(layerId == 3)
  238. {
  239. sortingOrder = typeCfg.thirdlLayer;
  240. }
  241. //清理旧的
  242. var spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, layerId);
  243. TryRemoveObj(parentObj, spritObjName);
  244. var objName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, layerId);
  245. TryRemoveObj(parentObj, objName);
  246. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  247. //这里需要先添加静态图,防止加载动画有延迟,出现光头
  248. GameObject spriteObj = AddSpriteObj(res, ext, spritObjName, parentObj, sortingOrder, needSetMask);
  249. GameObject addAniObj = null;
  250. if (showAni)
  251. {
  252. addAniObj = AddAnimationObj(res, objName, parentObj, sortingOrder);
  253. if (addAniObj != null && spriteObj != null)
  254. {
  255. Timers.inst.Add(0.03f, 1, (obj) =>
  256. {
  257. if (parentObj != null && parentObj.transform != null)
  258. {
  259. Transform tf = parentObj.transform.Find(spritObjName);
  260. if (tf != null && tf.gameObject != null && tf.gameObject.activeInHierarchy)
  261. {
  262. var assetDisposer = tf.gameObject.GetComponent<AssetReleaser>();
  263. if (assetDisposer != null)
  264. {
  265. if (!string.IsNullOrEmpty(assetDisposer.resPath))
  266. {
  267. string resPath = ResPathUtil.GetDressUpPath(res, ext);
  268. if (assetDisposer.resPath == resPath)
  269. {
  270. TryRemoveObj(parentObj, spritObjName);
  271. }
  272. }
  273. }
  274. }
  275. }
  276. });
  277. }
  278. }
  279. objName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, layerId);
  280. TryRemoveObj(parentObj, objName);
  281. TryAddEffectObj(res, objName, parentObj, sortingOrder, addAniObj != null);
  282. }
  283. private static GameObject AddSpriteObj(string res, string ext, string objName, GameObject parentObj, int sortingOrder, bool needSetMask)
  284. {
  285. string resPath = ResPathUtil.GetDressUpPath(res, ext);
  286. if (!VEngine.Versions.Contains(resPath))
  287. {
  288. return null;
  289. }
  290. if (CheckGameObjExisted(parentObj, objName, resPath))
  291. {
  292. return null;
  293. }
  294. SpriteRenderer spr = null;
  295. var gameObj = parentObj.transform.Find(objName)?.gameObject;
  296. if (gameObj == null)
  297. {
  298. gameObj = new GameObject(objName);
  299. gameObj.transform.SetParent(parentObj.transform, false);
  300. AddAssetReleaser(gameObj, resPath);
  301. }
  302. spr = gameObj.GetComponent<SpriteRenderer>();
  303. if (spr == null)
  304. {
  305. spr = gameObj.AddComponent<SpriteRenderer>();
  306. }
  307. float tx, ty;
  308. LoadSpritePos(res, out tx, out ty);
  309. gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
  310. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  311. spr.sprite = sp;
  312. spr.sortingOrder = sortingOrder;
  313. if (needSetMask)
  314. {
  315. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  316. if (parentObj.transform.parent != null && parentObj.transform.parent.name == "targetRole")
  317. {
  318. spr.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;
  319. }
  320. else
  321. {
  322. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  323. }
  324. }
  325. else
  326. {
  327. spr.maskInteraction = SpriteMaskInteraction.None;
  328. }
  329. return gameObj;
  330. }
  331. private static GameObject AddAnimationObj(string res, string objName, GameObject parentObj, int sortingOrder)
  332. {
  333. string resPath = ResPathUtil.GetDressUpAnimationPath(res);
  334. if (!VEngine.Versions.Contains(resPath))
  335. {
  336. return null;
  337. }
  338. if(CheckGameObjExisted(parentObj, objName, resPath))
  339. {
  340. return null;
  341. }
  342. var prefab = GFGAsset.Load<GameObject>(resPath);
  343. var gameObj = GameObject.Instantiate(prefab);
  344. AddAssetReleaser(gameObj, resPath);
  345. if (objName == BODY_ANIMATION_NAME)
  346. {
  347. //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  348. parentObj.transform.SetPositionAndRotation(gameObj.transform.localPosition, gameObj.transform.localRotation);
  349. gameObj.transform.SetPositionAndRotation(new Vector3(), new Quaternion());
  350. }
  351. gameObj.name = objName;
  352. gameObj.transform.SetParent(parentObj.transform, false);
  353. var render = gameObj.GetComponent<CubismRenderController>();
  354. if (render == null && gameObj.transform.childCount > 0)
  355. {
  356. var childObj = gameObj.transform.GetChild(0);
  357. if (childObj != null)
  358. {
  359. render = childObj.GetComponent<CubismRenderController>();
  360. }
  361. }
  362. if (render != null && render.gameObject.activeSelf == true)
  363. {
  364. render.SortingOrder = sortingOrder;
  365. }
  366. SetRenderersOrder(gameObj, sortingOrder);
  367. return gameObj;
  368. }
  369. private static void TryAddEffectObj(string res, string objName, GameObject parentObj, int sortingOrder, bool inAniDir)
  370. {
  371. var resPath = ResPathUtil.GetDressUpEffectPath(res, inAniDir);
  372. if (!VEngine.Versions.Contains(resPath))
  373. {
  374. return;
  375. }
  376. if (CheckGameObjExisted(parentObj, objName, resPath))
  377. {
  378. return;
  379. }
  380. GameObject effPre = GFGAsset.Load<GameObject>(resPath);
  381. var gameObj = GameObject.Instantiate(effPre);
  382. AddAssetReleaser(gameObj, resPath);
  383. gameObj.transform.SetParent(parentObj.transform, false);
  384. gameObj.name = objName;
  385. var sortingGroup = gameObj.transform.GetComponent<SortingGroup>();
  386. if (sortingGroup != null)
  387. {
  388. GameObject.Destroy(sortingGroup);
  389. }
  390. SetRenderersOrder(gameObj, sortingOrder + 1);//特效层默认高一个层级
  391. }
  392. private static bool TryRemoveObj(GameObject parentObj, string objName)
  393. {
  394. if (parentObj == null)
  395. {
  396. return false;
  397. }
  398. Transform transform = parentObj.transform.Find(objName);
  399. if (transform != null)
  400. {
  401. GameObject gameObj = transform.gameObject;
  402. if (gameObj != null)
  403. {
  404. GameObject.DestroyImmediate(gameObj);
  405. return true;
  406. }
  407. }
  408. return false;
  409. }
  410. private static bool CheckGameObjExisted(GameObject parentObj, string objName, string resPath)
  411. {
  412. if (parentObj == null)
  413. {
  414. return false;
  415. }
  416. Transform transform = parentObj.transform.Find(objName);
  417. if (transform != null)
  418. {
  419. GameObject gameObj = transform.gameObject;
  420. if (gameObj != null)
  421. {
  422. var assetReleaser = gameObj.GetComponent<AssetReleaser>();
  423. if (assetReleaser != null)
  424. {
  425. if (assetReleaser.resPath == resPath)
  426. {
  427. return true;
  428. }
  429. }
  430. }
  431. }
  432. return false;
  433. }
  434. public static GameObject CreateAnimationObj(string resPath)
  435. {
  436. // string resPath = ResPathUtil.GetCardAnimationPath(res);
  437. var prefab = GFGAsset.Load<GameObject>(resPath);
  438. if (prefab == null)
  439. {
  440. return null;
  441. }
  442. var gameObj = GameObject.Instantiate(prefab);
  443. AddAssetReleaser(gameObj, resPath);
  444. return gameObj;
  445. }
  446. public static void LoadSpritePos(string res, out float tx, out float ty)
  447. {
  448. string resPath = ResPathUtil.GetDressUpPath(res, "bytes");
  449. if (VEngine.Versions.Contains(resPath))
  450. {
  451. var asset = GFGAsset.Load<TextAsset>(resPath);
  452. if (asset != null)
  453. {
  454. var st = new MemoryStream(asset.bytes);
  455. var br = new BinaryReader(st);
  456. tx = br.ReadInt32() / 100f;
  457. ty = -br.ReadInt32() / 100f;
  458. GFGAsset.Release(resPath);
  459. return;
  460. }
  461. }
  462. tx = 0;
  463. ty = 0;
  464. }
  465. public static void SetRenderersOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  466. {
  467. var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
  468. for (int i = 0; i < meshRenderers.Length; i++)
  469. {
  470. var renderer = meshRenderers[i].GetComponent<Renderer>();
  471. if (renderer != null)
  472. {
  473. if (isAdd)
  474. {
  475. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  476. }
  477. else
  478. {
  479. renderer.sortingOrder = sortingOrder;
  480. }
  481. }
  482. }
  483. ParticleSystem[] particles = gameObj.transform.GetComponentsInChildren<ParticleSystem>();
  484. for (int i = 0; i < particles.Length; i++)
  485. {
  486. var renderer = particles[i].GetComponent<Renderer>();
  487. if (renderer != null)
  488. {
  489. if (isAdd)
  490. {
  491. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  492. }
  493. else
  494. {
  495. renderer.sortingOrder = sortingOrder;
  496. }
  497. }
  498. }
  499. }
  500. public static void SetSpriteRenderSortingOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  501. {
  502. SpriteRenderer[] spriteRenders = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
  503. for (int i = 0; i < spriteRenders.Length; i++)
  504. {
  505. if (isAdd)
  506. {
  507. spriteRenders[i].sortingOrder = spriteRenders[i].sortingOrder + sortingOrder;
  508. }
  509. else
  510. {
  511. spriteRenders[i].sortingOrder = sortingOrder;
  512. }
  513. }
  514. }
  515. }
  516. }