PhotographSceneManager.cs 11 KB

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