PhotographDataManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 DressUpObj dressUpObj;//当前穿戴的角色数据
  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. dressUpObj?.Dispose();
  70. dressUpObj = new DressUpObj();
  71. _equipSceneData.Clear();
  72. for (int i = 0; i < MyDressUpHelper.dressUpObj.itemList.Count; i++)
  73. {
  74. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(MyDressUpHelper.dressUpObj.itemList[i]);
  75. if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING || itemCfg.subType == ConstDressUpItemType.FEN_WEI)
  76. {
  77. PhotographDataManager.Instance.AddEquipItem(_equipSceneData, itemCfg.id, out _equipSceneData);
  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. if (_listData.IndexOf(ConstItemID.BORDERID) < 0)
  107. {
  108. _listData.Insert(0, ConstItemID.BORDERID);
  109. }
  110. break;
  111. case EnumPhotographType.EFFECT:
  112. _listData = PhotographDataManager.Instance.listEffectData;
  113. if (_listData.IndexOf(ConstItemID.EffectID) < 0)
  114. {
  115. _listData.Insert(0, ConstItemID.EffectID);
  116. }
  117. break;
  118. }
  119. return _listData;
  120. }
  121. //是否点击在UI上
  122. public bool IsTouchUI(GComponent viewCom)
  123. {
  124. GObject obj = GRoot.inst.touchTarget;
  125. UI_PhotographUI _viewCom = UI_PhotographUI.Proxy(viewCom);
  126. 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;
  127. }
  128. public void SetLayer(GameObject hitGameObj, string state)
  129. {
  130. if (state != "refresh")
  131. {
  132. int index = itemGameObjs.IndexOf(hitGameObj);
  133. if (state != "top")
  134. {
  135. if (index < 0)
  136. {
  137. PromptController.Instance.ShowFloatTextPrompt("未选中任物品");
  138. return;
  139. }
  140. if (index == 0 && state == "down")
  141. {
  142. PromptController.Instance.ShowFloatTextPrompt("已经在最下层了");
  143. return;
  144. }
  145. if (index == itemGameObjs.Count - 1 && state == "up")
  146. {
  147. PromptController.Instance.ShowFloatTextPrompt("已经在最上层了");
  148. return;
  149. }
  150. }
  151. GameObject gameObject = itemGameObjs[index];
  152. itemGameObjs.RemoveAt(index);
  153. if (state == "up")
  154. {
  155. itemGameObjs.Insert((index + 1), gameObject);
  156. }
  157. else if (state == "down")
  158. {
  159. itemGameObjs.Insert((index - 1), gameObject);
  160. }
  161. else if (state == "top")
  162. {
  163. itemGameObjs.Add(gameObject);
  164. }
  165. else
  166. {
  167. PromptController.Instance.ShowFloatTextPrompt(state + "操作失败");
  168. return;
  169. }
  170. }
  171. for (int i = 0; i < itemGameObjs.Count; i++)
  172. {
  173. PhotographDataManager.Instance.ChangeLayer(itemGameObjs[i], i * 100, state);
  174. }
  175. }
  176. private void ChangeLayer(GameObject parentObj, int layer, string state)
  177. {
  178. int count = 0;
  179. if (state == "up" || state == "top")
  180. {
  181. count = layer - GetMinLayer(parentObj);
  182. }
  183. else if (state == "down")
  184. {
  185. count = layer - GetMaxLayer(parentObj);
  186. }
  187. int maxLayer = int.MinValue;
  188. SpriteRenderer[] sps = parentObj.GetComponentsInChildren<SpriteRenderer>();
  189. for (int i = 0; i < sps.Length; i++)
  190. {
  191. sps[i].sortingOrder = sps[i].sortingOrder + layer + count;
  192. if (maxLayer < sps[i].sortingOrder)
  193. {
  194. maxLayer = sps[i].sortingOrder;
  195. }
  196. }
  197. for (int i = 0; i < parentObj.transform.childCount; i++)
  198. {
  199. Transform tf = parentObj.transform.GetChild(i);
  200. string[] strs = tf.name.Split('_');
  201. if (strs.Length > 1 && strs[1] == "eff")//子物体是特效
  202. {
  203. DressUpUtil.SetParticleSortingOrder(tf.gameObject, maxLayer);
  204. }
  205. }
  206. }
  207. private int GetMaxLayer(GameObject parentObj)
  208. {
  209. int layer = int.MinValue;
  210. for (int i = 0; i < parentObj.transform.childCount; i++)
  211. {
  212. Transform tf = parentObj.transform.GetChild(i);
  213. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  214. if (sp && layer < sp.sortingOrder)
  215. {
  216. layer = sp.sortingOrder;
  217. }
  218. }
  219. return layer;
  220. }
  221. public int GetMinLayer(GameObject parentObj)
  222. {
  223. int layer = int.MaxValue;
  224. for (int i = 0; i < parentObj.transform.childCount; i++)
  225. {
  226. Transform tf = parentObj.transform.GetChild(i);
  227. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  228. if (sp && sp.sortingOrder < layer)
  229. {
  230. layer = sp.sortingOrder;
  231. }
  232. }
  233. return layer;
  234. }
  235. public void SetBgPos(GameObject hitGameObj, Vector2 uiSize)
  236. {
  237. Vector2 size = hitGameObj.GetComponent<SpriteRenderer>().size;
  238. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  239. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  240. Vector2 pos = hitGameObj.transform.position;
  241. if (pos.x <= -deviationWidth)
  242. {
  243. hitGameObj.transform.position = new Vector2(-deviationWidth, hitGameObj.transform.position.y);
  244. }
  245. if (pos.x >= deviationWidth)
  246. {
  247. hitGameObj.transform.position = new Vector2(deviationWidth, hitGameObj.transform.position.y);
  248. }
  249. if (pos.y <= -deviationHeigh)
  250. {
  251. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, -deviationHeigh);
  252. }
  253. if (pos.y >= deviationHeigh)
  254. {
  255. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, deviationHeigh);
  256. }
  257. }
  258. public void AddItemGameObject(GameObject parentGameObj, bool setLayer)
  259. {
  260. itemGameObjs.Add(parentGameObj);
  261. if (setLayer)
  262. {
  263. int index = itemGameObjs.Count - 1;
  264. PhotographDataManager.Instance.ChangeLayer(itemGameObjs[index], index * 100, "up");
  265. }
  266. itemGameObjs.Sort((GameObject a, GameObject b) =>
  267. {
  268. int layerA = PhotographDataManager.Instance.GetMinLayer(a);
  269. int layerB = PhotographDataManager.Instance.GetMinLayer(b);
  270. if (layerA < layerB)
  271. {
  272. return -1;
  273. }
  274. else if (layerA > layerB)
  275. {
  276. return 1;
  277. }
  278. return 0;
  279. });
  280. }
  281. /// <summary>
  282. /// 将照片保存到本地
  283. /// </summary>
  284. public void SavePicturoToLocal(byte[] bytes, string fileName)
  285. {
  286. string path = Application.persistentDataPath + "/Pictures/WanshiJing/";
  287. //判断目录是否存在,不存在则会创建目录
  288. if (!Directory.Exists(path))
  289. {
  290. try
  291. {
  292. Directory.CreateDirectory(path);
  293. }
  294. catch (Exception exception)
  295. {
  296. throw new Exception("创建文件夹失败, error:" + exception.Message);
  297. }
  298. }
  299. var filePath = path + fileName;
  300. // byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  301. File.WriteAllBytes(filePath, bytes);
  302. UpdateSystemPhoto(filePath);
  303. }
  304. //调用iOS或Android原生方法保存图片后更新相册.
  305. private void UpdateSystemPhoto(string filePath)
  306. {
  307. #if UNITY_ANDROID
  308. AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  309. androidJavaObject.Call("scanFile", filePath, "已保存至本地"); //这里我们可以设置保存成功弹窗内容
  310. #endif
  311. }
  312. }
  313. }