DressUpUtil.cs 21 KB

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