PhotographDataManager.cs 12 KB

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