DressUpUtil.cs 21 KB

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