DressUpUtil.cs 21 KB

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