DressUpUtil.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 sceneObj, GameObject parentObj = null)
  90. {
  91. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  92. if (itemCfg != null)
  93. {
  94. if (parentObj == null)
  95. {
  96. if (itemCfg.subType == ConstDressUpItemType.BEI_JING || DressUpMenuItemCfg1Array.Instance.CheckIsSceneTypeBySubType(itemCfg.subType))
  97. {
  98. parentObj = sceneObj;
  99. }
  100. else
  101. {
  102. //角色
  103. Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
  104. parentObj = role.gameObject;
  105. }
  106. }
  107. string spritObjName;
  108. string aniObjName;
  109. //默认层
  110. if (!string.IsNullOrEmpty(itemCfg.resLayer1))
  111. {
  112. spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 1);
  113. TryRemoveSprite(parentObj, spritObjName);
  114. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 1);
  115. TryRemoveObj(parentObj, aniObjName);
  116. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, 1);
  117. TryRemoveObj(parentObj, aniObjName);
  118. }
  119. //特殊层
  120. if (!string.IsNullOrEmpty(itemCfg.resLayer2))
  121. {
  122. spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 2);
  123. TryRemoveSprite(parentObj, spritObjName);
  124. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 2);
  125. TryRemoveObj(parentObj, aniObjName);
  126. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, 2);
  127. TryRemoveObj(parentObj, aniObjName);
  128. }
  129. //第三层
  130. if (!string.IsNullOrEmpty(itemCfg.resLayer3))
  131. {
  132. spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 3);
  133. TryRemoveSprite(parentObj, spritObjName);
  134. aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 3);
  135. TryRemoveObj(parentObj, aniObjName);
  136. aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, 3);
  137. TryRemoveObj(parentObj, aniObjName);
  138. }
  139. }
  140. }
  141. public static DressUpRemoveOperation RemoveItemAsync(int itemID, GameObject sceneObj, GameObject parentObj = null)
  142. {
  143. var operation = new DressUpRemoveOperation(itemID, sceneObj, 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. string objName;
  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. objName = BODY_ANIMATION_NAME;
  186. //特效
  187. effectResPath = ResPathUtil.GetDressUpEffectPath(actionRes, true);
  188. if (!YooAssets.CheckResExist(effectResPath))
  189. {
  190. effectResPath = null;
  191. }
  192. }
  193. else
  194. {
  195. resPath = ResPathUtil.GetDressUpPath(BODY_DEFAULT_RES_NAME, extPng);
  196. objName = BODY_SPRITE_NAME;
  197. }
  198. if (GetGameObjExisted(parentObj, objName, resPath) != null || !YooAssets.CheckResExist(resPath))
  199. {
  200. return null;
  201. }
  202. DressUpLayerOperation handler = new DressUpLayerOperation(parentObj, needSetMask, showAni, resPath, effectResPath);
  203. handler.InitBody();
  204. handler.Begin();
  205. return handler;
  206. }
  207. private static DressUpLayerOperation UpdateLayerResAsync(ItemCfg itemCfg, GameObject parentObj, int layerId, bool needSetMask, bool showAni = true)
  208. {
  209. string res = ResPathUtil.GetDressUpItemLayerRes(itemCfg, layerId);
  210. string resPath;
  211. string aniResPath = null;
  212. //根据资源存在与否再次检查是否播放动画
  213. if (showAni)
  214. {
  215. aniResPath = ResPathUtil.GetDressUpAnimationPath(res);
  216. if(!YooAssets.CheckResExist(aniResPath))
  217. {
  218. showAni = false;
  219. }
  220. }
  221. string objName;
  222. if (showAni)
  223. {
  224. resPath = aniResPath;
  225. objName = string.Format(DressUpUtil.FORMAT_ANIMATION_NAME, itemCfg.subType, layerId);
  226. }
  227. else
  228. {
  229. resPath = ResPathUtil.GetDressUpPath(itemCfg, layerId);
  230. objName = string.Format(DressUpUtil.FORMAT_SPRITE_NAME, itemCfg.subType, layerId);
  231. }
  232. var gameObj = DressUpUtil.GetGameObjExisted(parentObj, objName, resPath);
  233. if (gameObj != null || !YooAssets.CheckResExist(resPath))
  234. {
  235. return null;
  236. }
  237. //特效
  238. string effectResPath = ResPathUtil.GetDressUpEffectPath(res, showAni);
  239. if (!YooAssets.CheckResExist(effectResPath))
  240. {
  241. effectResPath = null;
  242. }
  243. DressUpLayerOperation handler = new DressUpLayerOperation(parentObj, needSetMask, showAni, resPath, effectResPath);
  244. handler.InitLayer(itemCfg, layerId);
  245. handler.Begin();
  246. return handler;
  247. }
  248. public static GameObject AddSpriteObj(string resPath, string objName, GameObject parentObj, int sortingOrder, bool needSetMask)
  249. {
  250. if (!YooAssets.CheckResExist(resPath))
  251. {
  252. return null;
  253. }
  254. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  255. if (gameObj != null)
  256. {
  257. return gameObj;
  258. }
  259. SpriteRenderer spr = null;
  260. gameObj = parentObj.transform.Find(objName)?.gameObject;
  261. if (gameObj == null)
  262. {
  263. gameObj = new GameObject(objName);
  264. gameObj.transform.SetParent(parentObj.transform, false);
  265. }
  266. spr = gameObj.GetComponent<SpriteRenderer>();
  267. if (spr == null)
  268. {
  269. spr = gameObj.AddComponent<SpriteRenderer>();
  270. }
  271. float tx, ty;
  272. LoadSpritePos(resPath, out tx, out ty);
  273. gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
  274. SpriteHelper.AddSpriteTo(spr, resPath);
  275. gameObj.SetActive(true);
  276. spr.sortingOrder = sortingOrder;
  277. if (needSetMask)
  278. {
  279. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  280. if (parentObj.transform.parent != null && parentObj.transform.parent.name == "targetRole")
  281. {
  282. spr.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;
  283. }
  284. else
  285. {
  286. spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
  287. }
  288. }
  289. else
  290. {
  291. spr.maskInteraction = SpriteMaskInteraction.None;
  292. }
  293. return gameObj;
  294. }
  295. public static GameObject AddAnimationObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
  296. {
  297. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  298. if (gameObj != null)
  299. {
  300. return gameObj;
  301. }
  302. gameObj = PrefabManager.Instance.SpawnSync(resPath);
  303. if (gameObj == null)
  304. {
  305. return null;
  306. }
  307. if (objName == BODY_ANIMATION_NAME)
  308. {
  309. DressUpBodyOffsetInfo dressUpBodyOffsetInfo = gameObj.GetComponent<DressUpBodyOffsetInfo>();
  310. if(dressUpBodyOffsetInfo == null)
  311. {
  312. dressUpBodyOffsetInfo = gameObj.AddComponent<DressUpBodyOffsetInfo>();
  313. dressUpBodyOffsetInfo.OffsetPosition = gameObj.transform.localPosition;
  314. dressUpBodyOffsetInfo.Rotation = gameObj.transform.localRotation;
  315. }
  316. //如果是动作动画,就根据动画位置及角度信息设置给Role对象
  317. parentObj.transform.localPosition = dressUpBodyOffsetInfo.OffsetPosition;
  318. parentObj.transform.rotation = dressUpBodyOffsetInfo.Rotation;
  319. gameObj.transform.localPosition = Vector3.zero;
  320. gameObj.transform.rotation = Quaternion.identity;
  321. //.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
  322. }
  323. gameObj.name = objName;
  324. gameObj.transform.SetParent(parentObj.transform, false);
  325. var render = gameObj.GetComponent<CubismRenderController>();
  326. if (render == null && gameObj.transform.childCount > 0)
  327. {
  328. var childObj = gameObj.transform.GetChild(0);
  329. if (childObj != null)
  330. {
  331. render = childObj.GetComponent<CubismRenderController>();
  332. }
  333. }
  334. if (render != null && render.gameObject.activeSelf == true)
  335. {
  336. render.SortingOrder = sortingOrder;
  337. }
  338. SetRenderersOrder(gameObj, sortingOrder);
  339. return gameObj;
  340. }
  341. public static GameObject TryAddEffectObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
  342. {
  343. var gameObj = GetGameObjExisted(parentObj, objName, resPath);
  344. if (gameObj != null)
  345. {
  346. return gameObj;
  347. }
  348. gameObj = PrefabManager.Instance.SpawnSync(resPath);
  349. if (gameObj == null)
  350. {
  351. return null;
  352. }
  353. gameObj.transform.SetParent(parentObj.transform, false);
  354. gameObj.name = objName;
  355. var sortingGroup = gameObj.transform.GetComponent<SortingGroup>();
  356. if (sortingGroup != null)
  357. {
  358. GameObject.Destroy(sortingGroup);
  359. }
  360. SetRenderersOrder(gameObj, sortingOrder + 1);//特效层默认高一个层级
  361. return gameObj;
  362. }
  363. public static bool TryRemoveObj(GameObject parentObj, string objName)
  364. {
  365. if (parentObj == null)
  366. {
  367. return false;
  368. }
  369. Transform transform = parentObj.transform.Find(objName);
  370. if (transform != null)
  371. {
  372. GameObject gameObj = transform.gameObject;
  373. if (gameObj != null)
  374. {
  375. PrefabManager.Instance.Restore(gameObj);
  376. return true;
  377. }
  378. }
  379. return false;
  380. }
  381. public static bool TryRemoveSprite(GameObject parentObj, string objName)
  382. {
  383. if (parentObj == null)
  384. {
  385. return false;
  386. }
  387. Transform transform = parentObj.transform.Find(objName);
  388. if (transform != null)
  389. {
  390. GameObject gameObj = transform.gameObject;
  391. if (gameObj != null)
  392. {
  393. var spr = gameObj.GetComponent<SpriteRenderer>();
  394. if(spr != null)
  395. {
  396. //Debug.Log($"TryRemoveSprite {objName}");
  397. SpriteHelper.RemoveSpriteFrom(spr);
  398. gameObj.SetActive(false);
  399. }
  400. return true;
  401. }
  402. }
  403. return false;
  404. }
  405. public static GameObject GetGameObjExisted(GameObject parentObj, string objName, string resPath)
  406. {
  407. if (parentObj == null)
  408. {
  409. return null;
  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 assetReleaser = gameObj.GetComponent<AssetReleaser>();
  418. if (assetReleaser != null)
  419. {
  420. if (assetReleaser.ResPath == resPath)
  421. {
  422. return gameObj;
  423. }
  424. }
  425. }
  426. }
  427. return null;
  428. }
  429. public static void LoadSpritePos(string resPath, out float tx, out float ty)
  430. {
  431. int index = resPath.LastIndexOf('.');
  432. resPath = resPath.Substring(0, index) + ".bytes";
  433. if (YooAssets.CheckResExist(resPath))
  434. {
  435. var handle = YooAssets.LoadAssetSync<TextAsset>(resPath);
  436. TextAsset asset = handle.AssetObject as TextAsset;
  437. var st = new MemoryStream(asset.bytes);
  438. var br = new BinaryReader(st);
  439. tx = br.ReadInt32() / 100f;
  440. ty = -br.ReadInt32() / 100f;
  441. handle.Release();
  442. }
  443. else
  444. {
  445. tx = 0;
  446. ty = 0;
  447. }
  448. }
  449. private static void SetRenderersOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
  450. {
  451. var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
  452. for (int i = 0; i < meshRenderers.Length; i++)
  453. {
  454. var renderer = meshRenderers[i].GetComponent<Renderer>();
  455. if (renderer != null)
  456. {
  457. if (isAdd)
  458. {
  459. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  460. }
  461. else
  462. {
  463. renderer.sortingOrder = sortingOrder;
  464. }
  465. }
  466. }
  467. ParticleSystem[] particles = gameObj.transform.GetComponentsInChildren<ParticleSystem>();
  468. for (int i = 0; i < particles.Length; i++)
  469. {
  470. var renderer = particles[i].GetComponent<Renderer>();
  471. if (renderer != null)
  472. {
  473. if (isAdd)
  474. {
  475. renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
  476. }
  477. else
  478. {
  479. renderer.sortingOrder = sortingOrder;
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }