DressUpUtil.cs 19 KB

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