PhotographDataManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using ET;
  5. using FairyGUI;
  6. using UI.DressUp;
  7. using UnityEngine;
  8. namespace GFGGame
  9. {
  10. public enum EnumPhotographType
  11. {
  12. BG,
  13. NPC,
  14. SCENE,
  15. BORDER,
  16. EFFECT
  17. }
  18. public class PhotographDataManager : SingletonBase<PhotographDataManager>
  19. {
  20. public List<GameObject> itemGameObjs = new List<GameObject>();
  21. public List<int> _equipRoleData = new List<int>();//当前穿戴的角色数据
  22. public Dictionary<int, List<int>> _equipSceneData = new Dictionary<int, List<int>>();//当前穿戴的场景数据
  23. public List<int> listBgData = new List<int>();
  24. public List<int> listNpcData = new List<int>();
  25. public List<int> listSceneData = new List<int>();
  26. public List<int> listBorderData = new List<int>();
  27. public List<int> listEffectData = new List<int>();
  28. public void Clear()
  29. {
  30. listBgData.Clear();
  31. listNpcData.Clear();
  32. listSceneData.Clear();
  33. listBorderData.Clear();
  34. listEffectData.Clear();
  35. }
  36. public void Add(int itemID)
  37. {
  38. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  39. if (itemCfg.itemType == ConstItemType.DRESS_UP)
  40. {
  41. if (itemCfg.subType == ConstDressUpItemType.BEI_JING)
  42. {
  43. if (listBgData.IndexOf(itemID) < 0) listBgData.Add(itemID);
  44. }
  45. else if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING)
  46. {
  47. if (listSceneData.IndexOf(itemID) < 0) listSceneData.Add(itemID);
  48. }
  49. }
  50. else if (itemCfg.itemType == ConstItemType.PHOTOGRAPH)
  51. {
  52. if (itemCfg.subType == ConstItemSubType.NPC)
  53. {
  54. if (listNpcData.IndexOf(itemID) < 0) listNpcData.Add(itemID);
  55. }
  56. else if (itemCfg.subType == ConstItemSubType.BOREDR)
  57. {
  58. if (listBorderData.IndexOf(itemID) < 0) listBorderData.Add(itemID);
  59. }
  60. else if (itemCfg.subType == ConstItemSubType.EFFECT)
  61. {
  62. if (listEffectData.IndexOf(itemID) < 0) listEffectData.Add(itemID);
  63. }
  64. }
  65. }
  66. //将穿戴数据分类
  67. public void ClassifyEquipData()
  68. {
  69. _equipRoleData.Clear();
  70. _equipSceneData.Clear();
  71. for (int i = 0; i < EquipDataCache.cacher.equipDatas.Count; i++)
  72. {
  73. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(EquipDataCache.cacher.equipDatas[i]);
  74. if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING || itemCfg.subType == ConstDressUpItemType.FEN_WEI)
  75. {
  76. PhotographDataManager.Instance.AddEquipItem(_equipSceneData, itemCfg.id, out _equipSceneData);
  77. }
  78. else
  79. {
  80. _equipRoleData.Add(EquipDataCache.cacher.equipDatas[i]);
  81. }
  82. }
  83. }
  84. public void AddEquipItem(Dictionary<int, List<int>> _equipSceneData, int itemID, out Dictionary<int, List<int>> equipSceneData)
  85. {
  86. if (!_equipSceneData.ContainsKey(itemID))
  87. {
  88. _equipSceneData.Add(itemID, new List<int>());
  89. }
  90. _equipSceneData[itemID].Add(itemID);
  91. equipSceneData = _equipSceneData;
  92. }
  93. public List<int> GetListData(EnumPhotographType index)
  94. {
  95. List<int> _listData = null;
  96. switch (index)
  97. {
  98. case EnumPhotographType.BG:
  99. _listData = PhotographDataManager.Instance.listBgData;
  100. break;
  101. case EnumPhotographType.NPC:
  102. _listData = PhotographDataManager.Instance.listNpcData;
  103. break;
  104. case EnumPhotographType.SCENE:
  105. _listData = PhotographDataManager.Instance.listSceneData;
  106. break;
  107. case EnumPhotographType.BORDER:
  108. _listData = PhotographDataManager.Instance.listBorderData;
  109. if (_listData.IndexOf(ConstItemID.BORDERID) < 0)
  110. {
  111. _listData.Insert(0, ConstItemID.BORDERID);
  112. }
  113. break;
  114. case EnumPhotographType.EFFECT:
  115. _listData = PhotographDataManager.Instance.listEffectData;
  116. if (_listData.IndexOf(ConstItemID.EffectID) < 0)
  117. {
  118. _listData.Insert(0, ConstItemID.EffectID);
  119. }
  120. break;
  121. }
  122. return _listData;
  123. }
  124. //是否点击在UI上
  125. public bool IsTouchUI(GComponent viewCom)
  126. {
  127. GObject obj = GRoot.inst.touchTarget;
  128. UI_PhotographUI _viewCom = UI_PhotographUI.Proxy(viewCom);
  129. 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 || _viewCom.m_btnGalleryJoin.GetChild("icon").asLoader == obj;
  130. }
  131. public void SetLayer(GameObject hitGameObj, string state)
  132. {
  133. if (state != "refresh")
  134. {
  135. int index = itemGameObjs.IndexOf(hitGameObj);
  136. if (state != "top")
  137. {
  138. if (index < 0)
  139. {
  140. PromptController.Instance.ShowFloatTextPrompt("未选中任物品");
  141. return;
  142. }
  143. if (index == 0 && state == "down")
  144. {
  145. PromptController.Instance.ShowFloatTextPrompt("已经在最下层了");
  146. return;
  147. }
  148. if (index == itemGameObjs.Count - 1 && state == "up")
  149. {
  150. PromptController.Instance.ShowFloatTextPrompt("已经在最上层了");
  151. return;
  152. }
  153. }
  154. GameObject gameObject = itemGameObjs[index];
  155. itemGameObjs.RemoveAt(index);
  156. if (state == "up")
  157. {
  158. itemGameObjs.Insert((index + 1), gameObject);
  159. }
  160. else if (state == "down")
  161. {
  162. itemGameObjs.Insert((index - 1), gameObject);
  163. }
  164. else if (state == "top")
  165. {
  166. itemGameObjs.Add(gameObject);
  167. }
  168. else
  169. {
  170. PromptController.Instance.ShowFloatTextPrompt(state + "操作失败");
  171. return;
  172. }
  173. }
  174. for (int i = 0; i < itemGameObjs.Count; i++)
  175. {
  176. PhotographDataManager.Instance.ChangeLayer(itemGameObjs[i], i * 100, state);
  177. }
  178. }
  179. private void ChangeLayer(GameObject parentObj, int layer, string state)
  180. {
  181. int count = 0;
  182. if (state == "up" || state == "top")
  183. {
  184. count = layer - GetMinLayer(parentObj);
  185. }
  186. else if (state == "down")
  187. {
  188. count = layer - GetMaxLayer(parentObj);
  189. }
  190. int maxLayer = int.MinValue;
  191. SpriteRenderer[] sps = parentObj.GetComponentsInChildren<SpriteRenderer>();
  192. for (int i = 0; i < sps.Length; i++)
  193. {
  194. sps[i].sortingOrder = sps[i].sortingOrder + layer + count;
  195. if (maxLayer < sps[i].sortingOrder)
  196. {
  197. maxLayer = sps[i].sortingOrder;
  198. }
  199. }
  200. for (int i = 0; i < parentObj.transform.childCount; i++)
  201. {
  202. Transform tf = parentObj.transform.GetChild(i);
  203. string[] strs = tf.name.Split('_');
  204. if (strs.Length > 1 && strs[1] == "eff")//子物体是特效
  205. {
  206. DressUpUtil.SetParticleSortingOrder(tf.gameObject, maxLayer);
  207. }
  208. }
  209. }
  210. private int GetMaxLayer(GameObject parentObj)
  211. {
  212. int layer = int.MinValue;
  213. for (int i = 0; i < parentObj.transform.childCount; i++)
  214. {
  215. Transform tf = parentObj.transform.GetChild(i);
  216. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  217. if (sp && layer < sp.sortingOrder)
  218. {
  219. layer = sp.sortingOrder;
  220. }
  221. }
  222. return layer;
  223. }
  224. public int GetMinLayer(GameObject parentObj)
  225. {
  226. int layer = int.MaxValue;
  227. for (int i = 0; i < parentObj.transform.childCount; i++)
  228. {
  229. Transform tf = parentObj.transform.GetChild(i);
  230. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  231. if (sp && sp.sortingOrder < layer)
  232. {
  233. layer = sp.sortingOrder;
  234. }
  235. }
  236. return layer;
  237. }
  238. public void SetBgPos(GameObject hitGameObj, Vector2 uiSize)
  239. {
  240. Vector2 size = hitGameObj.GetComponent<SpriteRenderer>().size;
  241. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  242. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  243. Vector2 pos = hitGameObj.transform.position;
  244. if (pos.x <= -deviationWidth)
  245. {
  246. hitGameObj.transform.position = new Vector2(-deviationWidth, hitGameObj.transform.position.y);
  247. }
  248. if (pos.x >= deviationWidth)
  249. {
  250. hitGameObj.transform.position = new Vector2(deviationWidth, hitGameObj.transform.position.y);
  251. }
  252. if (pos.y <= -deviationHeigh)
  253. {
  254. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, -deviationHeigh);
  255. }
  256. if (pos.y >= deviationHeigh)
  257. {
  258. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, deviationHeigh);
  259. }
  260. }
  261. public void AddItemGameObject(GameObject parentGameObj, bool setLayer)
  262. {
  263. itemGameObjs.Add(parentGameObj);
  264. if (setLayer)
  265. {
  266. int index = itemGameObjs.Count - 1;
  267. PhotographDataManager.Instance.ChangeLayer(itemGameObjs[index], index * 100, "up");
  268. }
  269. itemGameObjs.Sort((GameObject a, GameObject b) =>
  270. {
  271. int layerA = PhotographDataManager.Instance.GetMinLayer(a);
  272. int layerB = PhotographDataManager.Instance.GetMinLayer(b);
  273. if (layerA < layerB)
  274. {
  275. return -1;
  276. }
  277. else if (layerA > layerB)
  278. {
  279. return 1;
  280. }
  281. return 0;
  282. });
  283. }
  284. /// <summary>
  285. /// 将照片保存到本地
  286. /// </summary>
  287. public void SavePicturoToLocal(byte[] bytes, string fileName)
  288. {
  289. string path = Application.persistentDataPath + "/Pictures/WanshiJing/";
  290. //判断目录是否存在,不存在则会创建目录
  291. if (!Directory.Exists(path))
  292. {
  293. try
  294. {
  295. Directory.CreateDirectory(path);
  296. }
  297. catch (Exception exception)
  298. {
  299. throw new Exception("创建文件夹失败, error:" + exception.Message);
  300. }
  301. }
  302. var filePath = path + fileName;
  303. // byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  304. File.WriteAllBytes(filePath, bytes);
  305. UpdateSystemPhoto(filePath);
  306. }
  307. //调用iOS或Android原生方法保存图片后更新相册.
  308. private void UpdateSystemPhoto(string filePath)
  309. {
  310. #if UNITY_ANDROID
  311. AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  312. androidJavaObject.Call("scanFile", filePath, "已保存至本地"); //这里我们可以设置保存成功弹窗内容
  313. #endif
  314. }
  315. }
  316. }