PhotographSceneManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. var typeItem = typeof(ItemCfg).GetField(layerName);
  103. string value = typeItem.GetValue(itemCfg).ToString();
  104. string res = value == "n" ? itemCfg.Res : string.Format("{0}_{1}", itemCfg.Res, value);
  105. string resPath = ResPathUtil.GetDressUpAnimationPath(res);
  106. var handler = DressUpUtil.AddItemAsync(itemId, sceneObj, false, true, parentGameObj, resLayer);
  107. if (handler != null)
  108. {
  109. foreach (var t in handler)
  110. {
  111. await t.Task;
  112. }
  113. foreach (var t in handler)
  114. {
  115. t.UpdateView();
  116. t.Release();
  117. }
  118. }
  119. if (YooAssetsEx.CheckResExist(resPath))
  120. {
  121. parentGameObj.transform.localPosition = Vector3.zero;
  122. }
  123. else
  124. {
  125. if (parentGameObj.transform.childCount > 0)
  126. {
  127. GameObject gameObject = parentGameObj.transform.GetChild(0).gameObject;
  128. SetBoxCollider2DToGameObject(gameObject);
  129. }
  130. }
  131. await PhotographUtil.Instance.SetGameObjectCenter(parentGameObj);
  132. }
  133. //拍照角色
  134. private void UpdatePhotographBody(GameObject sceneObj, GameObject parentObj)
  135. {
  136. PhotographDataManager.Instance.dressUpObj.setSceneObj(sceneObj, false, false, parentObj, false,
  137. async () => { SetRoleBoxCollider(parentObj); });
  138. PhotographDataManager.Instance.dressUpObj.PutOnDressUpData(MyDressUpHelper.dressUpObj.DressUpDataClone());
  139. }
  140. private async void SetRoleBoxCollider(GameObject parentObj)
  141. {
  142. for (int i = 0; i < parentObj.transform.childCount; i++)
  143. {
  144. Transform transform = parentObj.transform.GetChild(i);
  145. if (transform.gameObject.GetComponent<SpriteRenderer>() != null)
  146. {
  147. SetBoxCollider2DToGameObject(transform.gameObject);
  148. }
  149. }
  150. await PhotographUtil.Instance.SetGameObjectCenter(parentObj);
  151. }
  152. //向Transform添加SpriteRenderer并设置资源
  153. private void SetSpriteRendererToTransform(Transform tf, string resPath)
  154. {
  155. tf.position = Vector3.zero;
  156. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  157. if (spr == null)
  158. {
  159. tf.gameObject.AddComponent<SpriteRenderer>();
  160. spr = tf.GetComponent<SpriteRenderer>();
  161. }
  162. if (resPath != "")
  163. {
  164. SpriteHelper.AddSpriteTo(spr, resPath);
  165. spr.size = spr.sprite.bounds.size; //将节点设置为原图大小
  166. }
  167. else
  168. {
  169. spr.sprite = null;
  170. }
  171. }
  172. //向GameObject添加BoxCollider2D
  173. private void SetBoxCollider2DToGameObject(GameObject gameObject)
  174. {
  175. BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
  176. if (polygonCollider2D != null)
  177. {
  178. GameObject.Destroy(polygonCollider2D);
  179. }
  180. polygonCollider2D = gameObject.AddComponent<BoxCollider2D>();
  181. polygonCollider2D.isTrigger = true;
  182. }
  183. private void AddItemGameObjectToList(GameObject parentGameObj, bool setLayer)
  184. {
  185. List<GameObject> itemGameObjs = PhotographDataManager.Instance.itemGameObjs;
  186. bool hasSame = false;
  187. for (int i = 0; i < itemGameObjs.Count; i++)
  188. {
  189. if (itemGameObjs[i].name == parentGameObj.name && parentGameObj.name == "Npc")
  190. {
  191. hasSame = true;
  192. itemGameObjs[i] = parentGameObj;
  193. }
  194. else if (itemGameObjs[i].name == parentGameObj.name && parentGameObj.name == "Border")
  195. {
  196. hasSame = true;
  197. itemGameObjs[i] = parentGameObj;
  198. }
  199. }
  200. if (!hasSame)
  201. {
  202. itemGameObjs.Add(parentGameObj);
  203. }
  204. if (setLayer)
  205. {
  206. int index = itemGameObjs.Count - 1;
  207. PhotographUtil.Instance.ChangeLayer(itemGameObjs[index], index * PhotographDataManager.layerCount,
  208. "up");
  209. }
  210. itemGameObjs.Sort((GameObject a, GameObject b) =>
  211. {
  212. int layerA = PhotographUtil.Instance.GetMaxLayer(a);
  213. int layerB = PhotographUtil.Instance.GetMaxLayer(b);
  214. if (layerA < layerB)
  215. {
  216. return -1;
  217. }
  218. else if (layerA > layerB)
  219. {
  220. return 1;
  221. }
  222. return string.Compare(a.name, b.name);
  223. });
  224. }
  225. private List<GameObject> SortItemGameObjsLayer(List<GameObject> itemGameObjs)
  226. {
  227. itemGameObjs.Sort((GameObject a, GameObject b) =>
  228. {
  229. int layerA = PhotographUtil.Instance.GetMinLayer(a);
  230. int layerB = PhotographUtil.Instance.GetMinLayer(b);
  231. if (layerA < layerB)
  232. {
  233. return -1;
  234. }
  235. else if (layerA > layerB)
  236. {
  237. return 1;
  238. }
  239. return string.Compare(a.name, b.name);
  240. });
  241. return itemGameObjs;
  242. }
  243. //移除指定GameObject的BoxCollider2D
  244. public void DeleteBoxCollider2DFromGameObject(GameObject gameObject)
  245. {
  246. BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
  247. if (polygonCollider2D != null)
  248. {
  249. GameObject.Destroy(polygonCollider2D);
  250. }
  251. }
  252. //移除指定GameObject的SpriteRenderer
  253. public void DeleteSpriteRendererFromGameObject(GameObject gameObject)
  254. {
  255. SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
  256. if (spriteRenderer != null)
  257. {
  258. GameObject.Destroy(spriteRenderer);
  259. }
  260. }
  261. }
  262. }