PhotographSceneManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using cfg.GfgCfg;
  5. using UnityEngine;
  6. using YooAsset;
  7. namespace GFGGame
  8. {
  9. public class PhotographSceneManager : SingletonBase<PhotographSceneManager>
  10. {
  11. public GameObject sceneObject;
  12. public async void AddBgItem(ItemCfg itemCfg)
  13. {
  14. Transform tf = sceneObject.transform.Find("Bg/BgRes");
  15. string resPath = ResPathUtil.GetSceneBgPath(itemCfg.Res);
  16. await LoadManager.Instance.CheckResExsitedOrDownload(resPath);
  17. SetSpriteRendererToTransform(tf, resPath);
  18. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  19. spr.sortingOrder = -4000;
  20. SetBoxCollider2DToGameObject(tf.gameObject);
  21. }
  22. public void AddBorderItem(ItemCfg itemCfg)
  23. {
  24. Transform tf = sceneObject.transform.Find("Border/BorderRes");
  25. // 刷新父节点Border的数据
  26. tf.parent.position = Vector3.zero;
  27. tf.parent.eulerAngles = Vector3.zero;
  28. tf.parent.localScale = Vector3.one;
  29. string resPath;
  30. if (itemCfg.Id != ConstItemID.BORDERID)
  31. {
  32. resPath = ResPathUtil.GetPhotographBorderPath(itemCfg.Res, ItemUtil.GetItemResExt(itemCfg.ItemType, itemCfg.SubType));
  33. }
  34. // 默认无边框不用加载资源
  35. else
  36. {
  37. resPath = "";
  38. }
  39. SetSpriteRendererToTransform(tf, resPath);
  40. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  41. spr.sortingOrder = 10000;//边框在所有道具的上边
  42. SetBoxCollider2DToGameObject(tf.gameObject);
  43. }
  44. public void AddNpcItem(ItemCfg itemCfg)
  45. {
  46. Transform tf = sceneObject.transform.Find("Scene/Npc/NpcRes");
  47. string resPath = ResPathUtil.GetNpcPicFPath(itemCfg.Res);
  48. SetSpriteRendererToTransform(tf, resPath);
  49. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  50. tf.localPosition = new Vector2(-spr.bounds.center.x, -spr.bounds.center.y);
  51. SetBoxCollider2DToGameObject(tf.gameObject);
  52. AddItemGameObjectToList(tf.parent.gameObject, true);
  53. }
  54. public void AddBodyItem(int isLeft = 0)
  55. {
  56. GameObject bodyParent = sceneObject.transform.Find("Scene/Role").gameObject;
  57. if (isLeft == 1)
  58. {
  59. MatchingLeftDataManager.Instance.roleGameobj = bodyParent;
  60. }
  61. else if (isLeft == 2)
  62. {
  63. MatchingRightDataManager.Instance.roleGameobj = bodyParent;
  64. }
  65. else
  66. {
  67. MatchingCompetitionDataManager.Instance.roleGameobj = bodyParent;
  68. MatchingOneDataManager.Instance.roleGameobj = bodyParent;
  69. }
  70. UpdatePhotographBody(sceneObject, bodyParent);
  71. AddItemGameObjectToList(bodyParent, false);
  72. }
  73. public async Task AddSceneItem(GameObject parentGameObj, ItemCfg itemCfg, int layer, bool setLayer, bool isOnlyEff = false,int isLeft = 0)
  74. {
  75. if(isLeft == 1)
  76. {
  77. MatchingLeftDataManager.Instance.itemGameObjs.Add(parentGameObj);
  78. }
  79. else if(isLeft == 2)
  80. {
  81. MatchingRightDataManager.Instance.itemGameObjs.Add(parentGameObj);
  82. }
  83. else
  84. {
  85. MatchingCompetitionDataManager.Instance.itemGameObjs.Add(parentGameObj);
  86. MatchingOneDataManager.Instance.itemGameObjs.Add(parentGameObj);
  87. }
  88. await AddItemToScene(sceneObject, parentGameObj, itemCfg.Id, layer);
  89. string layerName = string.Format("resLayer{0}", layer);
  90. string value = typeof(ItemCfg).GetField(layerName).GetValue(itemCfg).ToString();
  91. AddItemGameObjectToList(parentGameObj, setLayer);
  92. }
  93. //拍照场景添加单个道具
  94. public async Task AddItemToScene(GameObject sceneObj, GameObject parentGameObj, int itemId, int resLayer)
  95. {
  96. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
  97. parentGameObj.transform.SetParent(sceneObj.transform.Find("Scene"), false);
  98. string layerName = string.Format("resLayer{0}", resLayer);
  99. string value = typeof(ItemCfg).GetField(layerName).GetValue(itemCfg).ToString();
  100. string res = value == "n" ? itemCfg.Res : string.Format("{0}_{1}", itemCfg.Res, value);
  101. string resPath = ResPathUtil.GetDressUpAnimationPath(res);
  102. var handler = DressUpUtil.AddItemAsync(itemId, sceneObj, false, true, parentGameObj, resLayer);
  103. if (handler != null)
  104. {
  105. foreach (var t in handler)
  106. {
  107. await t.Task;
  108. }
  109. foreach (var t in handler)
  110. {
  111. t.UpdateView();
  112. t.Release();
  113. }
  114. }
  115. if (YooAssets.CheckResExist(resPath))
  116. {
  117. parentGameObj.transform.localPosition = Vector3.zero;
  118. }
  119. else
  120. {
  121. if (parentGameObj.transform.childCount > 0)
  122. {
  123. GameObject gameObject = parentGameObj.transform.GetChild(0).gameObject;
  124. SetBoxCollider2DToGameObject(gameObject);
  125. }
  126. }
  127. await PhotographUtil.Instance.SetGameObjectCenter(parentGameObj);
  128. }
  129. //拍照角色
  130. private void UpdatePhotographBody(GameObject sceneObj, GameObject parentObj)
  131. {
  132. PhotographDataManager.Instance.dressUpObj.setSceneObj(sceneObj, false, false, parentObj, false, async ()=>
  133. {
  134. SetRoleBoxCollider(parentObj);
  135. });
  136. PhotographDataManager.Instance.dressUpObj.PutOnDressUpData(MyDressUpHelper.dressUpObj.DressUpDataClone());
  137. }
  138. private async void SetRoleBoxCollider(GameObject parentObj)
  139. {
  140. for (int i = 0; i < parentObj.transform.childCount; i++)
  141. {
  142. Transform transform = parentObj.transform.GetChild(i);
  143. if (transform.gameObject.GetComponent<SpriteRenderer>() != null)
  144. {
  145. SetBoxCollider2DToGameObject(transform.gameObject);
  146. }
  147. }
  148. await PhotographUtil.Instance.SetGameObjectCenter(parentObj);
  149. }
  150. //向Transform添加SpriteRenderer并设置资源
  151. private void SetSpriteRendererToTransform(Transform tf, string resPath)
  152. {
  153. tf.position = Vector3.zero;
  154. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  155. if (spr == null)
  156. {
  157. tf.gameObject.AddComponent<SpriteRenderer>();
  158. spr = tf.GetComponent<SpriteRenderer>();
  159. }
  160. if(resPath != "")
  161. {
  162. SpriteHelper.AddSpriteTo(spr, resPath);
  163. spr.size = spr.sprite.bounds.size;//将节点设置为原图大小
  164. }
  165. else
  166. {
  167. spr.sprite = null;
  168. }
  169. }
  170. //向GameObject添加BoxCollider2D
  171. private void SetBoxCollider2DToGameObject(GameObject gameObject)
  172. {
  173. BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
  174. if (polygonCollider2D != null)
  175. {
  176. GameObject.Destroy(polygonCollider2D);
  177. }
  178. polygonCollider2D = gameObject.AddComponent<BoxCollider2D>();
  179. polygonCollider2D.isTrigger = true;
  180. }
  181. private void AddItemGameObjectToList(GameObject parentGameObj, bool setLayer)
  182. {
  183. List<GameObject> itemGameObjs = PhotographDataManager.Instance.itemGameObjs;
  184. bool hasSame = false;
  185. for(int i =0; i<itemGameObjs.Count;i++)
  186. {
  187. if (itemGameObjs[i].name == parentGameObj.name && parentGameObj.name == "Npc")
  188. {
  189. hasSame = true;
  190. itemGameObjs[i] = parentGameObj;
  191. }
  192. else if (itemGameObjs[i].name == parentGameObj.name && parentGameObj.name == "Border")
  193. {
  194. hasSame = true;
  195. itemGameObjs[i] = parentGameObj;
  196. }
  197. }
  198. if(!hasSame)
  199. {
  200. itemGameObjs.Add(parentGameObj);
  201. }
  202. if (setLayer)
  203. {
  204. int index = itemGameObjs.Count - 1;
  205. PhotographUtil.Instance.ChangeLayer(itemGameObjs[index], index * PhotographDataManager.layerCount, "up");
  206. }
  207. itemGameObjs.Sort((GameObject a, GameObject b) =>
  208. {
  209. int layerA = PhotographUtil.Instance.GetMaxLayer(a);
  210. int layerB = PhotographUtil.Instance.GetMaxLayer(b);
  211. if (layerA < layerB)
  212. {
  213. return -1;
  214. }
  215. else if (layerA > layerB)
  216. {
  217. return 1;
  218. }
  219. return string.Compare(a.name, b.name);
  220. });
  221. }
  222. private List<GameObject> SortItemGameObjsLayer(List<GameObject> itemGameObjs)
  223. {
  224. itemGameObjs.Sort((GameObject a, GameObject b) =>
  225. {
  226. int layerA = PhotographUtil.Instance.GetMinLayer(a);
  227. int layerB = PhotographUtil.Instance.GetMinLayer(b);
  228. if (layerA < layerB)
  229. {
  230. return -1;
  231. }
  232. else if (layerA > layerB)
  233. {
  234. return 1;
  235. }
  236. return string.Compare(a.name, b.name);
  237. });
  238. return itemGameObjs;
  239. }
  240. //移除指定GameObject的BoxCollider2D
  241. public void DeleteBoxCollider2DFromGameObject(GameObject gameObject)
  242. {
  243. BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
  244. if (polygonCollider2D != null)
  245. {
  246. GameObject.Destroy(polygonCollider2D);
  247. }
  248. }
  249. //移除指定GameObject的SpriteRenderer
  250. public void DeleteSpriteRendererFromGameObject(GameObject gameObject)
  251. {
  252. SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
  253. if (spriteRenderer != null)
  254. {
  255. GameObject.Destroy(spriteRenderer);
  256. }
  257. }
  258. }
  259. }