DressUpUtil.cs 20 KB

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