PhotographDataManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System.Collections.Generic;
  2. using FairyGUI;
  3. using UI.DressUp;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public enum EnumPhotographType
  8. {
  9. BG,
  10. NPC,
  11. SCENE,
  12. BORDER,
  13. EFFECT
  14. }
  15. public class PhotographDataManager : SingletonBase<PhotographDataManager>
  16. {
  17. public List<GameObject> itemGameObjs = new List<GameObject>();
  18. public List<int> _equipRoleData = new List<int>();//当前穿戴的角色数据
  19. public Dictionary<int, List<int>> _equipSceneData = new Dictionary<int, List<int>>();//当前穿戴的场景数据
  20. public List<int> listBgData = new List<int>();
  21. public List<int> listNpcData = new List<int>();
  22. public List<int> listSceneData = new List<int>();
  23. public List<int> listBorderData = new List<int>();
  24. public List<int> listEffectData = new List<int>();
  25. public void Clear()
  26. {
  27. listBgData.Clear();
  28. listNpcData.Clear();
  29. listSceneData.Clear();
  30. listBorderData.Clear();
  31. listEffectData.Clear();
  32. }
  33. public void Add(int itemID)
  34. {
  35. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  36. if (itemCfg.itemType == ConstItemType.DRESS_UP)
  37. {
  38. if (itemCfg.subType == ConstDressUpItemType.BEI_JING)
  39. {
  40. if (listBgData.IndexOf(itemID) < 0) listBgData.Add(itemID);
  41. }
  42. else if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING)
  43. {
  44. if (listSceneData.IndexOf(itemID) < 0) listSceneData.Add(itemID);
  45. }
  46. }
  47. else if (itemCfg.itemType == ConstItemType.ITEM)
  48. {
  49. if (itemCfg.subType == ConstItemSubType.NPC)
  50. {
  51. if (listNpcData.IndexOf(itemID) < 0) listNpcData.Add(itemID);
  52. }
  53. else if (itemCfg.subType == ConstItemSubType.BOREDR)
  54. {
  55. if (listBorderData.IndexOf(itemID) < 0) listBorderData.Add(itemID);
  56. }
  57. else if (itemCfg.subType == ConstItemSubType.EFFECT)
  58. {
  59. if (listEffectData.IndexOf(itemID) < 0) listEffectData.Add(itemID);
  60. }
  61. }
  62. }
  63. //将穿戴数据分类
  64. public void ClassifyEquipData()
  65. {
  66. _equipRoleData.Clear();
  67. _equipSceneData.Clear();
  68. for (int i = 0; i < EquipDataCache.cacher.equipDatas.Length; i++)
  69. {
  70. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(EquipDataCache.cacher.equipDatas[i]);
  71. if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING || itemCfg.subType == ConstDressUpItemType.FEN_WEI)
  72. {
  73. PhotographDataManager.Instance.AddEquipItem(_equipSceneData, itemCfg.id, out _equipSceneData);
  74. }
  75. else
  76. {
  77. _equipRoleData.Add(EquipDataCache.cacher.equipDatas[i]);
  78. }
  79. }
  80. }
  81. public void AddEquipItem(Dictionary<int, List<int>> _equipSceneData, int itemID, out Dictionary<int, List<int>> equipSceneData)
  82. {
  83. if (!_equipSceneData.ContainsKey(itemID))
  84. {
  85. _equipSceneData.Add(itemID, new List<int>());
  86. }
  87. _equipSceneData[itemID].Add(itemID);
  88. equipSceneData = _equipSceneData;
  89. }
  90. public List<int> GetListData(EnumPhotographType index)
  91. {
  92. List<int> _listData = null;
  93. switch (index)
  94. {
  95. case EnumPhotographType.BG:
  96. _listData = PhotographDataManager.Instance.listBgData;
  97. break;
  98. case EnumPhotographType.NPC:
  99. _listData = PhotographDataManager.Instance.listNpcData;
  100. break;
  101. case EnumPhotographType.SCENE:
  102. _listData = PhotographDataManager.Instance.listSceneData;
  103. break;
  104. case EnumPhotographType.BORDER:
  105. _listData = PhotographDataManager.Instance.listBorderData;
  106. break;
  107. case EnumPhotographType.EFFECT:
  108. _listData = PhotographDataManager.Instance.listEffectData;
  109. break;
  110. }
  111. return _listData;
  112. }
  113. //是否点击在UI上
  114. public bool IsTouchUI(GComponent viewCom)
  115. {
  116. GObject obj = GRoot.inst.touchTarget;
  117. UI_PhotographUI _viewCom = UI_PhotographUI.Proxy(viewCom);
  118. return _viewCom.m_comSelectBox.m_btnSize.GetChild("icon").asLoader == obj || _viewCom.m_comSelectBox.m_btnDelete.GetChild("icon").asLoader == obj || _viewCom.m_comSelectBox.m_btnFlip.GetChild("icon").asLoader == obj || _viewCom.m_btnBack == obj || _viewCom.m_btnChoose.GetChild("icon").asLoader == obj || _viewCom.m_btnPhotograph.GetChild("icon").asLoader == obj || _viewCom.m_btnUp.GetChild("icon").asLoader == obj || _viewCom.m_btnDown.GetChild("icon").asLoader == obj;
  119. }
  120. public void SetLayer(GameObject hitGameObj, string state)
  121. {
  122. if (state != "refresh")
  123. {
  124. int index = itemGameObjs.IndexOf(hitGameObj);
  125. if (state != "top")
  126. {
  127. if (index < 0)
  128. {
  129. PromptController.Instance.ShowFloatTextPrompt("未选中任物品");
  130. return;
  131. }
  132. if (index == 0 && state == "down")
  133. {
  134. PromptController.Instance.ShowFloatTextPrompt("已经在最下层了");
  135. return;
  136. }
  137. if (index == itemGameObjs.Count - 1 && state == "up")
  138. {
  139. PromptController.Instance.ShowFloatTextPrompt("已经在最上层了");
  140. return;
  141. }
  142. }
  143. GameObject gameObject = itemGameObjs[index];
  144. itemGameObjs.RemoveAt(index);
  145. if (state == "up")
  146. {
  147. itemGameObjs.Insert((index + 1), gameObject);
  148. }
  149. else if (state == "down")
  150. {
  151. itemGameObjs.Insert((index - 1), gameObject);
  152. }
  153. else if (state == "top")
  154. {
  155. itemGameObjs.Add(gameObject);
  156. }
  157. else
  158. {
  159. PromptController.Instance.ShowFloatTextPrompt(state + "操作失败");
  160. return;
  161. }
  162. }
  163. for (int i = 0; i < itemGameObjs.Count; i++)
  164. {
  165. PhotographDataManager.Instance.ChangeLayer(itemGameObjs[i], i * 100, state);
  166. }
  167. }
  168. private void ChangeLayer(GameObject parentObj, int layer, string state)
  169. {
  170. int count = 0;
  171. if (state == "up" || state == "top")
  172. {
  173. count = layer - GetMinLayer(parentObj);
  174. }
  175. else if (state == "down")
  176. {
  177. count = layer - GetMaxLayer(parentObj);
  178. }
  179. int maxLayer = int.MinValue;
  180. SpriteRenderer[] sps = parentObj.GetComponentsInChildren<SpriteRenderer>();
  181. for (int i = 0; i < sps.Length; i++)
  182. {
  183. sps[i].sortingOrder = sps[i].sortingOrder + layer + count;
  184. if (maxLayer < sps[i].sortingOrder)
  185. {
  186. maxLayer = sps[i].sortingOrder;
  187. }
  188. }
  189. for (int i = 0; i < parentObj.transform.childCount; i++)
  190. {
  191. Transform tf = parentObj.transform.GetChild(i);
  192. string[] strs = tf.name.Split('_');
  193. if (strs.Length > 1 && strs[1] == "eff")//子物体是特效
  194. {
  195. DressUpUtil.SetParticleSortingOrder(tf.gameObject, maxLayer);
  196. }
  197. }
  198. }
  199. private int GetMaxLayer(GameObject parentObj)
  200. {
  201. int layer = int.MinValue;
  202. for (int i = 0; i < parentObj.transform.childCount; i++)
  203. {
  204. Transform tf = parentObj.transform.GetChild(i);
  205. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  206. if (sp && layer < sp.sortingOrder)
  207. {
  208. layer = sp.sortingOrder;
  209. }
  210. }
  211. return layer;
  212. }
  213. public int GetMinLayer(GameObject parentObj)
  214. {
  215. int layer = int.MaxValue;
  216. for (int i = 0; i < parentObj.transform.childCount; i++)
  217. {
  218. Transform tf = parentObj.transform.GetChild(i);
  219. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  220. if (sp && sp.sortingOrder < layer)
  221. {
  222. layer = sp.sortingOrder;
  223. }
  224. }
  225. return layer;
  226. }
  227. public void SetBgPos(GameObject hitGameObj, Vector2 uiSize)
  228. {
  229. Vector2 size = hitGameObj.GetComponent<SpriteRenderer>().size;
  230. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  231. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  232. Vector2 pos = hitGameObj.transform.position;
  233. if (pos.x <= -deviationWidth)
  234. {
  235. hitGameObj.transform.position = new Vector2(-deviationWidth, hitGameObj.transform.position.y);
  236. }
  237. if (pos.x >= deviationWidth)
  238. {
  239. hitGameObj.transform.position = new Vector2(deviationWidth, hitGameObj.transform.position.y);
  240. }
  241. if (pos.y <= -deviationHeigh)
  242. {
  243. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, -deviationHeigh);
  244. }
  245. if (pos.y >= deviationHeigh)
  246. {
  247. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, deviationHeigh);
  248. }
  249. }
  250. public void AddItemGameObject(GameObject parentGameObj, bool setLayer)
  251. {
  252. itemGameObjs.Add(parentGameObj);
  253. if (setLayer)
  254. {
  255. int index = itemGameObjs.Count - 1;
  256. PhotographDataManager.Instance.ChangeLayer(itemGameObjs[index], index * 100, "up");
  257. }
  258. itemGameObjs.Sort((GameObject a, GameObject b) =>
  259. {
  260. int layerA = PhotographDataManager.Instance.GetMinLayer(a);
  261. int layerB = PhotographDataManager.Instance.GetMinLayer(b);
  262. if (layerA < layerB)
  263. {
  264. return -1;
  265. }
  266. else if (layerA > layerB)
  267. {
  268. return 1;
  269. }
  270. return 0;
  271. });
  272. }
  273. }
  274. }