PhotographDataManager.cs 13 KB

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