DressUpUtil.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  351. if (gameObj != null)
  352. {
  353. return gameObj;
  354. }
  355. gameObj = CreateAnimationObj(resPath);
  356. if(gameObj == null)
  357. {
  358. return null;
  359. }
  360. if (objName == BODY_ANIMATION_NAME)
  361. {
  362. //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  363. parentObj.transform.SetPositionAndRotation(gameObj.transform.localPosition, gameObj.transform.localRotation);
  364. gameObj.transform.SetPositionAndRotation(new Vector3(), new Quaternion());
  365. }
  366. gameObj.name = objName;
  367. gameObj.transform.SetParent(parentObj.transform, false);
  368. var render = gameObj.GetComponent<CubismRenderController>();
  369. if (render == null && gameObj.transform.childCount > 0)
  370. {
  371. var childObj = gameObj.transform.GetChild(0);
  372. if (childObj != null)
  373. {
  374. render = childObj.GetComponent<CubismRenderController>();
  375. }
  376. }
  377. if (render != null && render.gameObject.activeSelf == true)
  378. {
  379. render.SortingOrder = sortingOrder;
  380. }
  381. SetRenderersOrder(gameObj, sortingOrder);
  382. return gameObj;
  383. }
  384. private static GameObject TryAddEffectObj(string res, string objName, GameObject parentObj, int sortingOrder, bool inAniDir)
  385. {
  386. var resPath = ResPathUtil.GetDressUpEffectPath(res, inAniDir);
  387. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  388. if (gameObj != null)
  389. {
  390. return gameObj;
  391. }
  392. gameObj = CreateAnimationObj(resPath);
  393. if (gameObj == null)
  394. {
  395. return null;
  396. }
  397. gameObj.transform.SetParent(parentObj.transform, false);
  398. gameObj.name = objName;
  399. var sortingGroup = gameObj.transform.GetComponent<SortingGroup>();
  400. if (sortingGroup != null)
  401. {
  402. GameObject.Destroy(sortingGroup);
  403. }
  404. SetRenderersOrder(gameObj, sortingOrder + 1);//特效层默认高一个层级
  405. return gameObj;
  406. }
  407. private static bool TryRemoveObj(GameObject parentObj, string objName)
  408. {
  409. if (parentObj == null)
  410. {
  411. return false;
  412. }
  413. Transform transform = parentObj.transform.Find(objName);
  414. if (transform != null)
  415. {
  416. GameObject gameObj = transform.gameObject;
  417. if (gameObj != null)
  418. {
  419. GameObject.DestroyImmediate(gameObj);
  420. return true;
  421. }
  422. }
  423. return false;
  424. }
  425. private static GameObject GetGameObjExisted(GameObject parentObj, string objName, string resPath)
  426. {
  427. if (parentObj == null)
  428. {
  429. return null;
  430. }
  431. Transform transform = parentObj.transform.Find(objName);
  432. if (transform != null)
  433. {
  434. GameObject gameObj = transform.gameObject;
  435. if (gameObj != null)
  436. {
  437. var assetReleaser = gameObj.GetComponent<AssetReleaser>();
  438. if (assetReleaser != null)
  439. {
  440. if (assetReleaser.resPath == resPath)
  441. {
  442. return gameObj;
  443. }
  444. }
  445. }
  446. }
  447. return null;
  448. }
  449. public static GameObject CreateAnimationObj(string resPath)
  450. {
  451. if (!VEngine.Versions.Contains(resPath))
  452. {
  453. return null;
  454. }
  455. var prefab = GFGAsset.Load<GameObject>(resPath);
  456. var gameObj = GameObject.Instantiate(prefab);
  457. AddAssetReleaser(gameObj, resPath);
  458. return gameObj;
  459. }
  460. public static void LoadSpritePos(string res, out float tx, out float ty)
  461. {
  462. string resPath = ResPathUtil.GetDressUpPath(res, "bytes");
  463. if (VEngine.Versions.Contains(resPath))
  464. {
  465. var asset = GFGAsset.Load<TextAsset>(resPath);
  466. if (asset != null)
  467. {
  468. var st = new MemoryStream(asset.bytes);
  469. var br = new BinaryReader(st);
  470. tx = br.ReadInt32() / 100f;
  471. ty = -br.ReadInt32() / 100f;
  472. GFGAsset.Release(resPath);
  473. return;
  474. }
  475. }
  476. tx = 0;
  477. ty = 0;
  478. }
  479. public static void SetRenderersOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  480. {
  481. var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
  482. for (int i = 0; i < meshRenderers.Length; i++)
  483. {
  484. var renderer = meshRenderers[i].GetComponent<Renderer>();
  485. if (renderer != null)
  486. {
  487. if (isAdd)
  488. {
  489. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  490. }
  491. else
  492. {
  493. renderer.sortingOrder = sortingOrder;
  494. }
  495. }
  496. }
  497. ParticleSystem[] particles = gameObj.transform.GetComponentsInChildren<ParticleSystem>();
  498. for (int i = 0; i < particles.Length; i++)
  499. {
  500. var renderer = particles[i].GetComponent<Renderer>();
  501. if (renderer != null)
  502. {
  503. if (isAdd)
  504. {
  505. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  506. }
  507. else
  508. {
  509. renderer.sortingOrder = sortingOrder;
  510. }
  511. }
  512. }
  513. }
  514. public static void SetSpriteRenderSortingOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  515. {
  516. SpriteRenderer[] spriteRenders = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
  517. for (int i = 0; i < spriteRenders.Length; i++)
  518. {
  519. if (isAdd)
  520. {
  521. spriteRenders[i].sortingOrder = spriteRenders[i].sortingOrder + sortingOrder;
  522. }
  523. else
  524. {
  525. spriteRenders[i].sortingOrder = sortingOrder;
  526. }
  527. }
  528. }
  529. }
  530. }