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. 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 resPath = ResPathUtil.GetDressUpPath(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(resPath, "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(ResPathUtil.GetDressUpPath(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. private static void updateLayerRes(ItemCfg itemCfg, GameObject parentObj, int layerId, bool needSetMask, bool showAni = true)
  212. {
  213. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
  214. string res = ResPathUtil.GetDressUpItemLayerRes(itemCfg, layerId);
  215. string resPath = ResPathUtil.GetDressUpPath(itemCfg, layerId);
  216. int sortingOrder = typeCfg.defaultLayer;
  217. if (layerId == 2)
  218. {
  219. sortingOrder = typeCfg.specialLayer;
  220. }
  221. else if (layerId == 3)
  222. {
  223. sortingOrder = typeCfg.thirdlLayer;
  224. }
  225. //清理旧的
  226. var spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, layerId);
  227. TryRemoveObj(parentObj, spritObjName);
  228. var objName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, layerId);
  229. TryRemoveObj(parentObj, objName);
  230. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  231. GameObject spriteObj = null;
  232. if (DressUpMenuItemCfg1Array.Instance.CheckIsDefaultType(itemCfg.subType))
  233. {
  234. //默认类型的部件需要先添加静态图,防止加载动画有延迟,出现光头
  235. spriteObj = AddSpriteObj(resPath, ext, spritObjName, parentObj, sortingOrder, needSetMask);
  236. }
  237. GameObject addAniObj = null;
  238. if (showAni)
  239. {
  240. addAniObj = AddAnimationObj(res, objName, parentObj, sortingOrder);
  241. if (addAniObj != null && spriteObj != null)
  242. {
  243. var dressUpPart = addAniObj.GetComponent<DressUpPart>();
  244. if (dressUpPart == null)
  245. {
  246. dressUpPart = addAniObj.AddComponent<DressUpPart>();
  247. }
  248. dressUpPart.OnTimer = (obj) =>
  249. {
  250. if (parentObj != null && parentObj.transform != null)
  251. {
  252. Transform tf = parentObj.transform.Find(spritObjName);
  253. if (tf != null && tf.gameObject != null && tf.gameObject.activeInHierarchy)
  254. {
  255. var assetDisposer = tf.gameObject.GetComponent<AssetReleaser>();
  256. if (assetDisposer != null)
  257. {
  258. if (!string.IsNullOrEmpty(assetDisposer.resPath))
  259. {
  260. string resPath = ResPathUtil.GetDressUpPath(itemCfg, layerId);
  261. if (assetDisposer.resPath == resPath)
  262. {
  263. TryRemoveObj(parentObj, spritObjName);
  264. }
  265. }
  266. }
  267. }
  268. }
  269. };
  270. Timers.inst.Add(0.03f, 1, dressUpPart.OnTimer);
  271. }
  272. }
  273. if (spriteObj == null && addAniObj == null)
  274. {
  275. //如果两个都没有,就添加静态图
  276. spriteObj = AddSpriteObj(resPath, ext, spritObjName, parentObj, sortingOrder, needSetMask);
  277. }
  278. objName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, layerId);
  279. TryRemoveObj(parentObj, objName);
  280. TryAddEffectObj(res, objName, parentObj, sortingOrder, addAniObj != null);
  281. }
  282. private static GameObject AddSpriteObj(string resPath, string ext, string objName, GameObject parentObj, int sortingOrder, bool needSetMask)
  283. {
  284. if (!VEngine.Versions.Contains(resPath))
  285. {
  286. return null;
  287. }
  288. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  289. if (gameObj != null)
  290. {
  291. return gameObj;
  292. }
  293. SpriteRenderer spr = null;
  294. gameObj = parentObj.transform.Find(objName)?.gameObject;
  295. if (gameObj == null)
  296. {
  297. gameObj = new GameObject(objName);
  298. gameObj.transform.SetParent(parentObj.transform, false);
  299. AddAssetReleaser(gameObj, resPath);
  300. }
  301. spr = gameObj.GetComponent<SpriteRenderer>();
  302. if (spr == null)
  303. {
  304. spr = gameObj.AddComponent<SpriteRenderer>();
  305. }
  306. float tx, ty;
  307. LoadSpritePos(resPath, out tx, out ty);
  308. gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
  309. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  310. spr.sprite = sp;
  311. spr.sortingOrder = sortingOrder;
  312. if (needSetMask)
  313. {
  314. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  315. if (parentObj.transform.parent != null && parentObj.transform.parent.name == "targetRole")
  316. {
  317. spr.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;
  318. }
  319. else
  320. {
  321. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  322. }
  323. }
  324. else
  325. {
  326. spr.maskInteraction = SpriteMaskInteraction.None;
  327. }
  328. return gameObj;
  329. }
  330. private static GameObject AddAnimationObj(string res, string objName, GameObject parentObj, int sortingOrder)
  331. {
  332. string resPath = ResPathUtil.GetDressUpAnimationPath(res);
  333. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  334. if (gameObj != null)
  335. {
  336. return gameObj;
  337. }
  338. gameObj = CreateAnimationObj(resPath);
  339. if(gameObj == null)
  340. {
  341. return null;
  342. }
  343. if (objName == BODY_ANIMATION_NAME)
  344. {
  345. //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  346. parentObj.transform.SetPositionAndRotation(gameObj.transform.localPosition, gameObj.transform.localRotation);
  347. gameObj.transform.SetPositionAndRotation(new Vector3(), new Quaternion());
  348. }
  349. gameObj.name = objName;
  350. gameObj.transform.SetParent(parentObj.transform, false);
  351. var render = gameObj.GetComponent<CubismRenderController>();
  352. if (render == null && gameObj.transform.childCount > 0)
  353. {
  354. var childObj = gameObj.transform.GetChild(0);
  355. if (childObj != null)
  356. {
  357. render = childObj.GetComponent<CubismRenderController>();
  358. }
  359. }
  360. if (render != null && render.gameObject.activeSelf == true)
  361. {
  362. render.SortingOrder = sortingOrder;
  363. }
  364. SetRenderersOrder(gameObj, sortingOrder);
  365. return gameObj;
  366. }
  367. private static GameObject TryAddEffectObj(string res, string objName, GameObject parentObj, int sortingOrder, bool inAniDir)
  368. {
  369. var resPath = ResPathUtil.GetDressUpEffectPath(res, inAniDir);
  370. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  371. if (gameObj != null)
  372. {
  373. return gameObj;
  374. }
  375. gameObj = CreateAnimationObj(resPath);
  376. if (gameObj == null)
  377. {
  378. return null;
  379. }
  380. gameObj.transform.SetParent(parentObj.transform, false);
  381. gameObj.name = objName;
  382. var sortingGroup = gameObj.transform.GetComponent<SortingGroup>();
  383. if (sortingGroup != null)
  384. {
  385. GameObject.Destroy(sortingGroup);
  386. }
  387. SetRenderersOrder(gameObj, sortingOrder + 1);//特效层默认高一个层级
  388. return gameObj;
  389. }
  390. private static bool TryRemoveObj(GameObject parentObj, string objName)
  391. {
  392. if (parentObj == null)
  393. {
  394. return false;
  395. }
  396. Transform transform = parentObj.transform.Find(objName);
  397. if (transform != null)
  398. {
  399. GameObject gameObj = transform.gameObject;
  400. if (gameObj != null)
  401. {
  402. GameObject.DestroyImmediate(gameObj);
  403. return true;
  404. }
  405. }
  406. return false;
  407. }
  408. private static GameObject GetGameObjExisted(GameObject parentObj, string objName, string resPath)
  409. {
  410. if (parentObj == null)
  411. {
  412. return null;
  413. }
  414. Transform transform = parentObj.transform.Find(objName);
  415. if (transform != null)
  416. {
  417. GameObject gameObj = transform.gameObject;
  418. if (gameObj != null)
  419. {
  420. var assetReleaser = gameObj.GetComponent<AssetReleaser>();
  421. if (assetReleaser != null)
  422. {
  423. if (assetReleaser.resPath == resPath)
  424. {
  425. return gameObj;
  426. }
  427. }
  428. }
  429. }
  430. return null;
  431. }
  432. public static GameObject CreateAnimationObj(string resPath)
  433. {
  434. if (!VEngine.Versions.Contains(resPath))
  435. {
  436. return null;
  437. }
  438. var prefab = GFGAsset.Load<GameObject>(resPath);
  439. var gameObj = GameObject.Instantiate(prefab);
  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 (VEngine.Versions.Contains(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. }