DressUpUtil.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 (GetGameObjExisted(parentObj, BODY_ANIMATION_NAME, resPath) != null)
  167. {
  168. return;
  169. }
  170. }
  171. else
  172. {
  173. string resPath = ResPathUtil.GetDressUpPath(BODY_DEFAULT_RES_NAME, extPng);
  174. if (GetGameObjExisted(parentObj, BODY_SPRITE_NAME, resPath) != null)
  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. var addAniObj = AddAnimationObj(actionRes, BODY_ANIMATION_NAME, parentObj, 0);
  185. //特效
  186. TryAddEffectObj(actionRes, BODY_EFFECT_OBJ_NAME, parentObj, 0, addAniObj != null);
  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. GameObject spriteObj = null;
  248. if (DressUpMenuItemCfg1Array.Instance.CheckIsDefaultType(itemCfg.subType))
  249. {
  250. //默认类型的部件需要先添加静态图,防止加载动画有延迟,出现光头
  251. spriteObj = AddSpriteObj(res, ext, spritObjName, parentObj, sortingOrder, needSetMask);
  252. }
  253. GameObject addAniObj = null;
  254. if (showAni)
  255. {
  256. addAniObj = AddAnimationObj(res, objName, parentObj, sortingOrder);
  257. if (addAniObj != null && spriteObj != null)
  258. {
  259. var dressUpPart = addAniObj.GetComponent<DressUpPart>();
  260. if (dressUpPart == null)
  261. {
  262. dressUpPart = addAniObj.AddComponent<DressUpPart>();
  263. }
  264. dressUpPart.OnTimer = (obj) =>
  265. {
  266. if (parentObj != null && parentObj.transform != null)
  267. {
  268. Transform tf = parentObj.transform.Find(spritObjName);
  269. if (tf != null && tf.gameObject != null && tf.gameObject.activeInHierarchy)
  270. {
  271. var assetDisposer = tf.gameObject.GetComponent<AssetReleaser>();
  272. if (assetDisposer != null)
  273. {
  274. if (!string.IsNullOrEmpty(assetDisposer.resPath))
  275. {
  276. string resPath = ResPathUtil.GetDressUpPath(res, ext);
  277. if (assetDisposer.resPath == resPath)
  278. {
  279. TryRemoveObj(parentObj, spritObjName);
  280. }
  281. }
  282. }
  283. }
  284. }
  285. };
  286. Timers.inst.Add(0.03f, 1, dressUpPart.OnTimer);
  287. }
  288. }
  289. if (spriteObj == null && addAniObj == null)
  290. {
  291. //如果两个都没有,就添加静态图
  292. spriteObj = AddSpriteObj(res, ext, spritObjName, parentObj, sortingOrder, needSetMask);
  293. }
  294. objName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, layerId);
  295. TryRemoveObj(parentObj, objName);
  296. TryAddEffectObj(res, objName, parentObj, sortingOrder, addAniObj != null);
  297. }
  298. private static GameObject AddSpriteObj(string res, string ext, string objName, GameObject parentObj, int sortingOrder, bool needSetMask)
  299. {
  300. string resPath = ResPathUtil.GetDressUpPath(res, ext);
  301. if (!VEngine.Versions.Contains(resPath))
  302. {
  303. return null;
  304. }
  305. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  306. if (gameObj != null)
  307. {
  308. return gameObj;
  309. }
  310. SpriteRenderer spr = null;
  311. gameObj = parentObj.transform.Find(objName)?.gameObject;
  312. if (gameObj == null)
  313. {
  314. gameObj = new GameObject(objName);
  315. gameObj.transform.SetParent(parentObj.transform, false);
  316. AddAssetReleaser(gameObj, resPath);
  317. }
  318. spr = gameObj.GetComponent<SpriteRenderer>();
  319. if (spr == null)
  320. {
  321. spr = gameObj.AddComponent<SpriteRenderer>();
  322. }
  323. float tx, ty;
  324. LoadSpritePos(res, out tx, out ty);
  325. gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
  326. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  327. spr.sprite = sp;
  328. spr.sortingOrder = sortingOrder;
  329. if (needSetMask)
  330. {
  331. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  332. if (parentObj.transform.parent != null && parentObj.transform.parent.name == "targetRole")
  333. {
  334. spr.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;
  335. }
  336. else
  337. {
  338. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  339. }
  340. }
  341. else
  342. {
  343. spr.maskInteraction = SpriteMaskInteraction.None;
  344. }
  345. return gameObj;
  346. }
  347. private static GameObject AddAnimationObj(string res, string objName, GameObject parentObj, int sortingOrder)
  348. {
  349. string resPath = ResPathUtil.GetDressUpAnimationPath(res);
  350. if (!VEngine.Versions.Contains(resPath))
  351. {
  352. return null;
  353. }
  354. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  355. if (gameObj != null)
  356. {
  357. return gameObj;
  358. }
  359. var prefab = GFGAsset.Load<GameObject>(resPath);
  360. gameObj = GameObject.Instantiate(prefab);
  361. AddAssetReleaser(gameObj, resPath);
  362. if (objName == BODY_ANIMATION_NAME)
  363. {
  364. //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  365. parentObj.transform.SetPositionAndRotation(gameObj.transform.localPosition, gameObj.transform.localRotation);
  366. gameObj.transform.SetPositionAndRotation(new Vector3(), new Quaternion());
  367. }
  368. gameObj.name = objName;
  369. gameObj.transform.SetParent(parentObj.transform, false);
  370. var render = gameObj.GetComponent<CubismRenderController>();
  371. if (render == null && gameObj.transform.childCount > 0)
  372. {
  373. var childObj = gameObj.transform.GetChild(0);
  374. if (childObj != null)
  375. {
  376. render = childObj.GetComponent<CubismRenderController>();
  377. }
  378. }
  379. if (render != null && render.gameObject.activeSelf == true)
  380. {
  381. render.SortingOrder = sortingOrder;
  382. }
  383. SetRenderersOrder(gameObj, sortingOrder);
  384. return gameObj;
  385. }
  386. private static GameObject TryAddEffectObj(string res, string objName, GameObject parentObj, int sortingOrder, bool inAniDir)
  387. {
  388. var resPath = ResPathUtil.GetDressUpEffectPath(res, inAniDir);
  389. if (!VEngine.Versions.Contains(resPath))
  390. {
  391. return null;
  392. }
  393. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  394. if (gameObj != null)
  395. {
  396. return gameObj;
  397. }
  398. GameObject effPre = GFGAsset.Load<GameObject>(resPath);
  399. gameObj = GameObject.Instantiate(effPre);
  400. AddAssetReleaser(gameObj, resPath);
  401. gameObj.transform.SetParent(parentObj.transform, false);
  402. gameObj.name = objName;
  403. var sortingGroup = gameObj.transform.GetComponent<SortingGroup>();
  404. if (sortingGroup != null)
  405. {
  406. GameObject.Destroy(sortingGroup);
  407. }
  408. SetRenderersOrder(gameObj, sortingOrder + 1);//特效层默认高一个层级
  409. return gameObj;
  410. }
  411. private static bool TryRemoveObj(GameObject parentObj, string objName)
  412. {
  413. if (parentObj == null)
  414. {
  415. return false;
  416. }
  417. Transform transform = parentObj.transform.Find(objName);
  418. if (transform != null)
  419. {
  420. GameObject gameObj = transform.gameObject;
  421. if (gameObj != null)
  422. {
  423. GameObject.DestroyImmediate(gameObj);
  424. return true;
  425. }
  426. }
  427. return false;
  428. }
  429. private static GameObject GetGameObjExisted(GameObject parentObj, string objName, string resPath)
  430. {
  431. if (parentObj == null)
  432. {
  433. return null;
  434. }
  435. Transform transform = parentObj.transform.Find(objName);
  436. if (transform != null)
  437. {
  438. GameObject gameObj = transform.gameObject;
  439. if (gameObj != null)
  440. {
  441. var assetReleaser = gameObj.GetComponent<AssetReleaser>();
  442. if (assetReleaser != null)
  443. {
  444. if (assetReleaser.resPath == resPath)
  445. {
  446. return gameObj;
  447. }
  448. }
  449. }
  450. }
  451. return null;
  452. }
  453. public static GameObject CreateAnimationObj(string resPath)
  454. {
  455. // string resPath = ResPathUtil.GetCardAnimationPath(res);
  456. var prefab = GFGAsset.Load<GameObject>(resPath);
  457. if (prefab == null)
  458. {
  459. return null;
  460. }
  461. var gameObj = GameObject.Instantiate(prefab);
  462. AddAssetReleaser(gameObj, resPath);
  463. return gameObj;
  464. }
  465. public static void LoadSpritePos(string res, out float tx, out float ty)
  466. {
  467. string resPath = ResPathUtil.GetDressUpPath(res, "bytes");
  468. if (VEngine.Versions.Contains(resPath))
  469. {
  470. var asset = GFGAsset.Load<TextAsset>(resPath);
  471. if (asset != null)
  472. {
  473. var st = new MemoryStream(asset.bytes);
  474. var br = new BinaryReader(st);
  475. tx = br.ReadInt32() / 100f;
  476. ty = -br.ReadInt32() / 100f;
  477. GFGAsset.Release(resPath);
  478. return;
  479. }
  480. }
  481. tx = 0;
  482. ty = 0;
  483. }
  484. public static void SetRenderersOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  485. {
  486. var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
  487. for (int i = 0; i < meshRenderers.Length; i++)
  488. {
  489. var renderer = meshRenderers[i].GetComponent<Renderer>();
  490. if (renderer != null)
  491. {
  492. if (isAdd)
  493. {
  494. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  495. }
  496. else
  497. {
  498. renderer.sortingOrder = sortingOrder;
  499. }
  500. }
  501. }
  502. ParticleSystem[] particles = gameObj.transform.GetComponentsInChildren<ParticleSystem>();
  503. for (int i = 0; i < particles.Length; i++)
  504. {
  505. var renderer = particles[i].GetComponent<Renderer>();
  506. if (renderer != null)
  507. {
  508. if (isAdd)
  509. {
  510. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  511. }
  512. else
  513. {
  514. renderer.sortingOrder = sortingOrder;
  515. }
  516. }
  517. }
  518. }
  519. public static void SetSpriteRenderSortingOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  520. {
  521. SpriteRenderer[] spriteRenders = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
  522. for (int i = 0; i < spriteRenders.Length; i++)
  523. {
  524. if (isAdd)
  525. {
  526. spriteRenders[i].sortingOrder = spriteRenders[i].sortingOrder + sortingOrder;
  527. }
  528. else
  529. {
  530. spriteRenders[i].sortingOrder = sortingOrder;
  531. }
  532. }
  533. }
  534. }
  535. }