DressUpUtil.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. using UnityEngine;
  2. using Live2D.Cubism.Rendering;
  3. using System.IO;
  4. using UnityEngine.Rendering;
  5. using YooAsset;
  6. using System.Collections.Generic;
  7. using System;
  8. namespace GFGGame
  9. {
  10. //提供各种换装相关的方法
  11. public class DressUpUtil
  12. {
  13. public const string HEAD_DEFAULT_RES_NAME = "head";
  14. public const string BODY_DEFAULT_RES_NAME = "body";
  15. public const string ROLE_OBJ_NAME = "Role";
  16. public const string HEAD_SPRITE_NAME = "Head";
  17. public const string BODY_SPRITE_NAME = "Body";
  18. public const string BODY_ANIMATION_NAME = "Body_a";
  19. public const string BODY_EFFECT_OBJ_NAME = "Body_eff";
  20. public const string FORMAT_SPRITE_NAME = "I_{0}_s{1}";
  21. public const string FORMAT_ANIMATION_NAME = "I_{0}_a{1}";
  22. public const string FORMAT_EFFECT_OBJ_NAME = "I_{0}_e{1}";
  23. public static List<DressUpLayerOperation> AddItemAsync(int itemID, GameObject sceneObj, bool needSetMask = false, bool showAni = true, GameObject parentObj = null, int resLayer = 0)
  24. {
  25. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  26. if (itemCfg == null) return 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. List<DressUpLayerOperation> handlers = new List<DressUpLayerOperation>();
  42. DressUpLayerOperation handler;
  43. if (resLayer > 0)
  44. {
  45. string layerName = "";
  46. switch (resLayer)
  47. {
  48. case 1:
  49. layerName = itemCfg.resLayer1;
  50. break;
  51. case 2:
  52. layerName = itemCfg.resLayer2;
  53. break;
  54. case 3:
  55. layerName = itemCfg.resLayer3;
  56. break;
  57. }
  58. if (!string.IsNullOrEmpty(layerName))
  59. {
  60. handler = UpdateLayerResAsync(itemCfg, parentObj, resLayer, needSetMask, showAni);
  61. if (handler != null) handlers.Add(handler);
  62. }
  63. }
  64. else
  65. {
  66. //普通层
  67. if (!string.IsNullOrEmpty(itemCfg.resLayer1))
  68. {
  69. handler = UpdateLayerResAsync(itemCfg, parentObj, 1, needSetMask, showAni);
  70. if (handler != null) handlers.Add(handler);
  71. }
  72. //第二层
  73. if (!string.IsNullOrEmpty(itemCfg.resLayer2))
  74. {
  75. handler = UpdateLayerResAsync(itemCfg, parentObj, 2, needSetMask, showAni);
  76. if (handler != null) handlers.Add(handler);
  77. }
  78. //第三层
  79. if (!string.IsNullOrEmpty(itemCfg.resLayer3))
  80. {
  81. handler = UpdateLayerResAsync(itemCfg, parentObj, 3, needSetMask, showAni);
  82. if (handler != null) handlers.Add(handler);
  83. }
  84. }
  85. if(handlers.Count > 0)
  86. {
  87. return handlers;
  88. }
  89. return null;
  90. }
  91. public static string GetSpriteName(ItemCfg itemCfg, int layer)
  92. {
  93. if(itemCfg.subType == ConstDressUpItemType.BEI_JING)
  94. {
  95. return "Bg";
  96. }
  97. return string.Format(FORMAT_SPRITE_NAME, itemCfg.id, layer);
  98. }
  99. public static void RemoveItem(int itemID, GameObject parentObj)
  100. {
  101. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  102. if (itemCfg == null || parentObj== null) return;
  103. string spritObjName;
  104. string aniObjName;
  105. //默认层
  106. if (!string.IsNullOrEmpty(itemCfg.resLayer1))
  107. {
  108. spritObjName = GetSpriteName(itemCfg, 1);
  109. TryRemoveSprite(parentObj, spritObjName);
  110. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.id, 1);
  111. TryRemoveObj(parentObj, aniObjName);
  112. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.id, 1);
  113. TryRemoveObj(parentObj, aniObjName);
  114. }
  115. //特殊层
  116. if (!string.IsNullOrEmpty(itemCfg.resLayer2))
  117. {
  118. spritObjName = GetSpriteName(itemCfg, 2);
  119. TryRemoveSprite(parentObj, spritObjName);
  120. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.id, 2);
  121. TryRemoveObj(parentObj, aniObjName);
  122. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.id, 2);
  123. TryRemoveObj(parentObj, aniObjName);
  124. }
  125. //第三层
  126. if (!string.IsNullOrEmpty(itemCfg.resLayer3))
  127. {
  128. spritObjName = GetSpriteName(itemCfg, 3);
  129. TryRemoveSprite(parentObj, spritObjName);
  130. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.id, 3);
  131. TryRemoveObj(parentObj, aniObjName);
  132. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.id, 3);
  133. TryRemoveObj(parentObj, aniObjName);
  134. }
  135. }
  136. public static DressUpRemoveOperation RemoveItemAsync(int itemID, GameObject sceneObj, GameObject parentObj = null)
  137. {
  138. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  139. if (itemCfg == null) return null;
  140. if (parentObj == null)
  141. {
  142. if (itemCfg.subType == ConstDressUpItemType.BEI_JING || DressUpMenuItemCfg1Array.Instance.CheckIsSceneTypeBySubType(itemCfg.subType))
  143. {
  144. parentObj = sceneObj;
  145. }
  146. else
  147. {
  148. //角色
  149. Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
  150. parentObj = role.gameObject;
  151. }
  152. }
  153. var operation = new DressUpRemoveOperation(itemID, parentObj);
  154. operation.Begin();
  155. return operation;
  156. }
  157. public static DressUpLayerOperation UpdateHeadAsync(bool show, GameObject sceneObj, bool needSetMask = false, GameObject parentObj = null)
  158. {
  159. var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
  160. parentObj = parentObj == null ? roleTf.gameObject : parentObj;
  161. string res = null;
  162. string resPath = null;
  163. if(show)
  164. {
  165. res = HEAD_DEFAULT_RES_NAME;
  166. resPath = ResPathUtil.GetDressUpPath(res);
  167. }
  168. DressUpLayerOperation handler = new DressUpLayerOperation(parentObj, needSetMask, false, resPath, null);
  169. handler.InitHead();
  170. handler.Begin();
  171. return handler;
  172. }
  173. public static DressUpLayerOperation UpdateBodyAsync(string actionRes, GameObject sceneObj, bool needSetMask = false, GameObject parentObj = null, int itemIdLianYiQun = 0)
  174. {
  175. //角色
  176. var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
  177. parentObj = parentObj == null ? roleTf.gameObject : parentObj;
  178. var extPng = "png";
  179. string resPath = null;
  180. string effectResPath = null;
  181. bool showAni = !string.IsNullOrEmpty(actionRes);
  182. string aniResPath = null;
  183. if (showAni)
  184. {
  185. aniResPath = ResPathUtil.GetDressUpAnimationPath(actionRes);
  186. if(!YooAssets.CheckResExist(aniResPath))
  187. {
  188. showAni = false;
  189. }
  190. }
  191. if(showAni)
  192. {
  193. resPath = aniResPath;
  194. //特效
  195. effectResPath = ResPathUtil.GetDressUpEffectPath(actionRes, true);
  196. if (!YooAssets.CheckResExist(effectResPath))
  197. {
  198. effectResPath = null;
  199. }
  200. }
  201. else
  202. {
  203. bool hideBody = false;
  204. if(itemIdLianYiQun > 0)
  205. {
  206. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemIdLianYiQun);
  207. hideBody = itemCfg.HideBody > 0;
  208. }
  209. if(hideBody)
  210. {
  211. resPath = null;
  212. }
  213. else
  214. {
  215. resPath = ResPathUtil.GetDressUpPath(BODY_DEFAULT_RES_NAME, extPng);
  216. }
  217. }
  218. DressUpLayerOperation handler = new DressUpLayerOperation(parentObj, needSetMask, showAni, resPath, effectResPath);
  219. handler.InitBody();
  220. handler.Begin();
  221. return handler;
  222. }
  223. private static DressUpLayerOperation UpdateLayerResAsync(ItemCfg itemCfg, GameObject parentObj, int layerId, bool needSetMask, bool showAni = true)
  224. {
  225. string resPath;
  226. string aniResPath = null;
  227. //根据资源存在与否再次检查是否播放动画
  228. if (showAni)
  229. {
  230. aniResPath = ResPathUtil.GetDressUpLayerAnimationResPath(itemCfg, layerId);
  231. if(!YooAssets.CheckResExist(aniResPath))
  232. {
  233. showAni = false;
  234. }
  235. }
  236. if (showAni)
  237. {
  238. resPath = aniResPath;
  239. }
  240. else
  241. {
  242. resPath = ResPathUtil.GetDressUpLayerSpriteResPath(itemCfg, layerId);
  243. }
  244. if (!YooAssets.CheckResExist(resPath))
  245. {
  246. return null;
  247. }
  248. //特效
  249. string effectResPath = ResPathUtil.GetDressUpLayerEffectResPath(itemCfg, layerId, showAni);
  250. if (!YooAssets.CheckResExist(effectResPath))
  251. {
  252. effectResPath = null;
  253. }
  254. DressUpLayerOperation handler = new DressUpLayerOperation(parentObj, needSetMask, showAni, resPath, effectResPath);
  255. handler.InitLayer(itemCfg, layerId);
  256. handler.Begin();
  257. return handler;
  258. }
  259. public static GameObject AddSpriteObj(string resPath, string objName, GameObject parentObj, int sortingOrder, bool needSetMask)
  260. {
  261. if (!YooAssets.CheckResExist(resPath))
  262. {
  263. return null;
  264. }
  265. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  266. if (gameObj != null)
  267. {
  268. return gameObj;
  269. }
  270. SpriteRenderer spr = null;
  271. gameObj = parentObj.transform.Find(objName)?.gameObject;
  272. if (gameObj == null)
  273. {
  274. gameObj = new GameObject(objName);
  275. gameObj.transform.SetParent(parentObj.transform, false);
  276. }
  277. spr = gameObj.GetComponent<SpriteRenderer>();
  278. if (spr == null)
  279. {
  280. spr = gameObj.AddComponent<SpriteRenderer>();
  281. }
  282. float tx, ty;
  283. LoadSpritePos(resPath, out tx, out ty);
  284. gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
  285. SpriteHelper.AddSpriteTo(spr, resPath);
  286. gameObj.SetActive(true);
  287. spr.sortingOrder = sortingOrder;
  288. if (needSetMask)
  289. {
  290. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  291. if (parentObj.transform.parent != null && parentObj.transform.parent.name == "targetRole")
  292. {
  293. spr.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;
  294. }
  295. else
  296. {
  297. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  298. }
  299. }
  300. else
  301. {
  302. spr.maskInteraction = SpriteMaskInteraction.None;
  303. }
  304. return gameObj;
  305. }
  306. public static GameObject AddAnimationObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
  307. {
  308. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  309. if (gameObj != null)
  310. {
  311. return gameObj;
  312. }
  313. gameObj = PrefabManager.Instance.SpawnSync(resPath);
  314. if (gameObj == null)
  315. {
  316. return null;
  317. }
  318. if (objName == BODY_ANIMATION_NAME)
  319. {
  320. DressUpOffsetInfo dressUpOffsetInfo = gameObj.GetComponent<DressUpOffsetInfo>();
  321. if(dressUpOffsetInfo == null)
  322. {
  323. dressUpOffsetInfo = gameObj.AddComponent<DressUpOffsetInfo>();
  324. dressUpOffsetInfo.OffsetPosition = gameObj.transform.localPosition;
  325. dressUpOffsetInfo.Rotation = gameObj.transform.localRotation;
  326. }
  327. //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  328. parentObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
  329. parentObj.transform.rotation = dressUpOffsetInfo.Rotation;
  330. //.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
  331. }
  332. gameObj.transform.localPosition = Vector3.zero;
  333. gameObj.transform.rotation = Quaternion.identity;
  334. gameObj.name = objName;
  335. gameObj.transform.SetParent(parentObj.transform, false);
  336. var render = gameObj.GetComponent<CubismRenderController>();
  337. if (render == null && gameObj.transform.childCount > 0)
  338. {
  339. var childObj = gameObj.transform.GetChild(0);
  340. if (childObj != null)
  341. {
  342. render = childObj.GetComponent<CubismRenderController>();
  343. }
  344. }
  345. if (render != null && render.gameObject.activeSelf == true)
  346. {
  347. render.SortingOrder = sortingOrder;
  348. }
  349. SetRenderersOrder(gameObj, sortingOrder);
  350. return gameObj;
  351. }
  352. public static GameObject TryAddEffectObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
  353. {
  354. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  355. if (gameObj != null)
  356. {
  357. return gameObj;
  358. }
  359. gameObj = PrefabManager.Instance.SpawnSync(resPath);
  360. if (gameObj == null)
  361. {
  362. return null;
  363. }
  364. DressUpOffsetInfo dressUpOffsetInfo = gameObj.GetComponent<DressUpOffsetInfo>();
  365. if (dressUpOffsetInfo == null)
  366. {
  367. dressUpOffsetInfo = gameObj.AddComponent<DressUpOffsetInfo>();
  368. dressUpOffsetInfo.OffsetPosition = gameObj.transform.localPosition;
  369. dressUpOffsetInfo.Rotation = gameObj.transform.localRotation;
  370. }
  371. gameObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
  372. gameObj.transform.rotation = dressUpOffsetInfo.Rotation;
  373. gameObj.transform.SetParent(parentObj.transform, false);
  374. gameObj.name = objName;
  375. var sortingGroup = gameObj.transform.GetComponent<SortingGroup>();
  376. if (sortingGroup != null)
  377. {
  378. GameObject.Destroy(sortingGroup);
  379. }
  380. SetRenderersOrder(gameObj, sortingOrder + 1);//特效层默认高一个层级
  381. return gameObj;
  382. }
  383. public static bool TryRemoveObj(GameObject parentObj, string objName)
  384. {
  385. if (parentObj == null)
  386. {
  387. return false;
  388. }
  389. Transform transform = parentObj.transform.Find(objName);
  390. if (transform != null)
  391. {
  392. GameObject gameObj = transform.gameObject;
  393. if (gameObj != null)
  394. {
  395. PrefabManager.Instance.Restore(gameObj);
  396. return true;
  397. }
  398. }
  399. return false;
  400. }
  401. public static bool TryRemoveSprite(GameObject parentObj, string objName)
  402. {
  403. if (parentObj == null)
  404. {
  405. return false;
  406. }
  407. Transform transform = parentObj.transform.Find(objName);
  408. if (transform != null)
  409. {
  410. GameObject gameObj = transform.gameObject;
  411. if (gameObj != null)
  412. {
  413. var spr = gameObj.GetComponent<SpriteRenderer>();
  414. if(spr != null)
  415. {
  416. //LogUtil.LogEditor($"TryRemoveSprite {objName}");
  417. SpriteHelper.RemoveSpriteFrom(spr);
  418. gameObj.SetActive(false);
  419. }
  420. return true;
  421. }
  422. }
  423. return false;
  424. }
  425. public 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 void LoadSpritePos(string resPath, out float tx, out float ty)
  450. {
  451. int index = resPath.LastIndexOf('.');
  452. resPath = resPath.Substring(0, index) + ".bytes";
  453. if (YooAssets.CheckResExist(resPath))
  454. {
  455. var handle = YooAssets.LoadAssetSync<TextAsset>(resPath);
  456. TextAsset asset = handle.AssetObject as TextAsset;
  457. var st = new MemoryStream(asset.bytes);
  458. var br = new BinaryReader(st);
  459. tx = br.ReadInt32() / 100f;
  460. ty = -br.ReadInt32() / 100f;
  461. handle.Release();
  462. }
  463. else
  464. {
  465. tx = 0;
  466. ty = 0;
  467. }
  468. }
  469. public static void SetRenderersOrder(GameObject gameObj, int sortingOrder)
  470. {
  471. var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
  472. for (int i = 0; i < meshRenderers.Length; i++)
  473. {
  474. var renderer = meshRenderers[i].GetComponent<Renderer>();
  475. if (renderer != null)
  476. {
  477. renderer.sortingOrder = sortingOrder;
  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. renderer.sortingOrder = sortingOrder;
  487. }
  488. }
  489. var spritesRenderers = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
  490. for(int i = 0; i < spritesRenderers.Length; i++)
  491. {
  492. var spriteRender = spritesRenderers[i];
  493. spriteRender.sortingOrder = sortingOrder;
  494. }
  495. }
  496. public static List<int> GetSuitItems(int suitId, bool isAction, int[] excludeType = null, bool showOptional = true, bool CheckOwn = true)
  497. {
  498. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  499. if (suitCfg == null)
  500. {
  501. return null;
  502. }
  503. List<int> items = new List<int>(suitCfg.partsArr);
  504. if (showOptional)
  505. {
  506. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  507. {
  508. items.AddRange(suitCfg.partsOptionalArr);
  509. }
  510. }
  511. int subType = 0;
  512. //找到要穿的散件
  513. List<int> targetItemList = new List<int>();
  514. foreach (int itemId in items)
  515. {
  516. if (!CheckOwn || DressUpMenuItemDataManager.CheckHasItem(itemId))
  517. {
  518. subType = ItemDataManager.GetItemSubType(itemId);
  519. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  520. if (!isAction || notInAction)
  521. {
  522. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  523. {
  524. targetItemList.Add(itemId);
  525. }
  526. }
  527. }
  528. }
  529. return targetItemList;
  530. }
  531. }
  532. }