DressUpUtil.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. //这里有个需求是一些特殊动作需要自定义位置和角度,
  321. //实现方法就是美术在动作预制体上设置这个位置,
  322. //然后程序记录下这个位置,把他设置到role节点上,并把动作节点的位置重置成默认值。
  323. DressUpOffsetInfo dressUpOffsetInfo = gameObj.GetComponent<DressUpOffsetInfo>();
  324. if(dressUpOffsetInfo == null)
  325. {
  326. dressUpOffsetInfo = gameObj.AddComponent<DressUpOffsetInfo>();
  327. dressUpOffsetInfo.OffsetPosition = gameObj.transform.localPosition;
  328. dressUpOffsetInfo.Rotation = gameObj.transform.localRotation;
  329. }
  330. //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  331. parentObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
  332. parentObj.transform.rotation = dressUpOffsetInfo.Rotation;
  333. //.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
  334. }
  335. gameObj.transform.localPosition = Vector3.zero;
  336. gameObj.transform.rotation = Quaternion.identity;
  337. gameObj.name = objName;
  338. gameObj.transform.SetParent(parentObj.transform, false);
  339. var render = gameObj.GetComponent<CubismRenderController>();
  340. if (render == null && gameObj.transform.childCount > 0)
  341. {
  342. var childObj = gameObj.transform.GetChild(0);
  343. if (childObj != null)
  344. {
  345. render = childObj.GetComponent<CubismRenderController>();
  346. }
  347. }
  348. if (render != null && render.gameObject.activeSelf == true)
  349. {
  350. render.SortingOrder = sortingOrder;
  351. }
  352. SetRenderersOrder(gameObj, sortingOrder);
  353. return gameObj;
  354. }
  355. public static GameObject TryAddEffectObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
  356. {
  357. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  358. if (gameObj != null)
  359. {
  360. return gameObj;
  361. }
  362. gameObj = PrefabManager.Instance.SpawnSync(resPath);
  363. if (gameObj == null)
  364. {
  365. return null;
  366. }
  367. DressUpOffsetInfo dressUpOffsetInfo = gameObj.GetComponent<DressUpOffsetInfo>();
  368. if (dressUpOffsetInfo == null)
  369. {
  370. //这里因为特效资源初始是有位移偏移的,记录下来每次设置为初始位置和角度,以防unity自身的坐标转换导致位置偏移
  371. dressUpOffsetInfo = gameObj.AddComponent<DressUpOffsetInfo>();
  372. dressUpOffsetInfo.OffsetPosition = gameObj.transform.localPosition;
  373. dressUpOffsetInfo.Rotation = gameObj.transform.localRotation;
  374. }
  375. gameObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
  376. gameObj.transform.rotation = dressUpOffsetInfo.Rotation;
  377. gameObj.transform.SetParent(parentObj.transform, false);
  378. gameObj.name = objName;
  379. var sortingGroup = gameObj.transform.GetComponent<SortingGroup>();
  380. if (sortingGroup != null)
  381. {
  382. GameObject.Destroy(sortingGroup);
  383. }
  384. SetRenderersOrder(gameObj, sortingOrder + 1);//特效层默认高一个层级
  385. return gameObj;
  386. }
  387. public static bool TryRemoveObj(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. PrefabManager.Instance.Restore(gameObj);
  400. return true;
  401. }
  402. }
  403. return false;
  404. }
  405. public static bool TryRemoveSprite(GameObject parentObj, string objName)
  406. {
  407. if (parentObj == null)
  408. {
  409. return false;
  410. }
  411. Transform transform = parentObj.transform.Find(objName);
  412. if (transform != null)
  413. {
  414. GameObject gameObj = transform.gameObject;
  415. if (gameObj != null)
  416. {
  417. var spr = gameObj.GetComponent<SpriteRenderer>();
  418. if(spr != null)
  419. {
  420. //LogUtil.LogEditor($"TryRemoveSprite {objName}");
  421. SpriteHelper.RemoveSpriteFrom(spr);
  422. gameObj.SetActive(false);
  423. }
  424. return true;
  425. }
  426. }
  427. return false;
  428. }
  429. public static GameObject GetGameObjExisted(GameObject parentObj, string objName, string resPath)
  430. {
  431. if (parentObj == null)
  432. {
  433. return null;
  434. }
  435. Transform transform = parentObj.transform.Find(objName);
  436. if (transform != null)
  437. {
  438. GameObject gameObj = transform.gameObject;
  439. if (gameObj != null)
  440. {
  441. var assetReleaser = gameObj.GetComponent<AssetReleaser>();
  442. if (assetReleaser != null)
  443. {
  444. if (assetReleaser.ResPath == resPath)
  445. {
  446. return gameObj;
  447. }
  448. }
  449. }
  450. }
  451. return null;
  452. }
  453. public static void LoadSpritePos(string resPath, out float tx, out float ty)
  454. {
  455. int index = resPath.LastIndexOf('.');
  456. resPath = resPath.Substring(0, index) + ".bytes";
  457. if (YooAssets.CheckResExist(resPath))
  458. {
  459. var handle = YooAssets.LoadAssetSync<TextAsset>(resPath);
  460. TextAsset asset = handle.AssetObject as TextAsset;
  461. var st = new MemoryStream(asset.bytes);
  462. var br = new BinaryReader(st);
  463. tx = br.ReadInt32() / 100f;
  464. ty = -br.ReadInt32() / 100f;
  465. handle.Release();
  466. }
  467. else
  468. {
  469. tx = 0;
  470. ty = 0;
  471. }
  472. }
  473. public static void SetRenderersOrder(GameObject gameObj, int sortingOrder)
  474. {
  475. var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
  476. for (int i = 0; i < meshRenderers.Length; i++)
  477. {
  478. var renderer = meshRenderers[i].GetComponent<Renderer>();
  479. if (renderer != null)
  480. {
  481. renderer.sortingOrder = sortingOrder;
  482. }
  483. }
  484. ParticleSystem[] particles = gameObj.transform.GetComponentsInChildren<ParticleSystem>();
  485. for (int i = 0; i < particles.Length; i++)
  486. {
  487. var renderer = particles[i].GetComponent<Renderer>();
  488. if (renderer != null)
  489. {
  490. renderer.sortingOrder = sortingOrder;
  491. }
  492. }
  493. var spritesRenderers = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
  494. for(int i = 0; i < spritesRenderers.Length; i++)
  495. {
  496. var spriteRender = spritesRenderers[i];
  497. spriteRender.sortingOrder = sortingOrder;
  498. }
  499. }
  500. public static List<int> GetSuitItems(int suitId, bool isAction, int[] excludeType = null, bool showOptional = true, bool CheckOwn = true,bool isDress = false)
  501. {
  502. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  503. if (suitCfg == null)
  504. {
  505. return null;
  506. }
  507. List<int> items = new List<int>(suitCfg.partsArr);
  508. if (showOptional)
  509. {
  510. if (suitCfg.partsOptionalArr != null && suitCfg.partsOptionalArr.Length > 0)
  511. {
  512. items.AddRange(suitCfg.partsOptionalArr);
  513. }
  514. }
  515. int subType = 0;
  516. //找到要穿的散件
  517. List<int> targetItemList = new List<int>();
  518. foreach (int itemId in items)
  519. {
  520. if (!CheckOwn || DressUpMenuItemDataManager.CheckHasItem(itemId) || isDress)
  521. {
  522. subType = ItemDataManager.GetItemSubType(itemId);
  523. bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
  524. if (!isAction || notInAction)
  525. {
  526. if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
  527. {
  528. targetItemList.Add(itemId);
  529. }
  530. }
  531. }
  532. }
  533. return targetItemList;
  534. }
  535. }
  536. }