PaiZhaoDataManager.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using LitJson;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.Rendering;
  7. using XRTool.Util;
  8. using static XunJianDataManager;
  9. public class PaiZhaoDataManager : Singleton<PaiZhaoDataManager>
  10. {
  11. List<paizhaoData> paizhaoList = new List<paizhaoData>();
  12. public void addImage(Texture tex)
  13. {
  14. WindowsManager.Instance.StartCoroutine(GetTextureTo2D(tex));
  15. }
  16. IEnumerator GetTextureTo2D(Texture tex)
  17. {
  18. var req = AsyncGPUReadback.Request(tex,0, TextureFormat.ARGB32);
  19. yield return new WaitUntil(() => req.done);
  20. if (!req.hasError)
  21. {
  22. byte[] bts = new byte[req.layerDataSize];
  23. req.GetData<byte>().CopyTo(bts);
  24. Texture2D texture2D = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
  25. texture2D.LoadRawTextureData(bts);
  26. bts = texture2D.EncodeToPNG();
  27. WindowsManager.Instance.DestroyText2D(texture2D);
  28. Debug.Log("准备存文件===》" + bts.Length);
  29. int count = 0;
  30. if(Directory.Exists(Application.persistentDataPath + "/PaiZhao/" + XunJianDataManager.Instance.chooseXunJian.id + "/" + XunJianDataManager.Instance.chooseXunJian.nowIndex))
  31. count= Directory.GetFiles(Application.persistentDataPath + "/PaiZhao/" + XunJianDataManager.Instance.chooseXunJian.id + "/" + XunJianDataManager.Instance.chooseXunJian.nowIndex).Length;
  32. //文件流信息
  33. //StreamWriter sw;
  34. Stream sw;
  35. string name = count+".png";
  36. string path = Application.persistentDataPath + "/PaiZhao/" + XunJianDataManager.Instance.chooseXunJian.id + "/" + XunJianDataManager.Instance.chooseXunJian.nowIndex + "/" + name;
  37. Debug.Log("准备存文件===》" + path);
  38. FileInfo file = new FileInfo(path);
  39. try
  40. {
  41. if (file.Exists)
  42. {
  43. file.Delete();
  44. }
  45. if (!Directory.Exists(Application.persistentDataPath + "/PaiZhao"))
  46. {
  47. Directory.CreateDirectory(Application.persistentDataPath + "/PaiZhao");
  48. }
  49. if (!Directory.Exists(Application.persistentDataPath + "/PaiZhao/" + XunJianDataManager.Instance.chooseXunJian.id))
  50. {
  51. Directory.CreateDirectory(Application.persistentDataPath + "/PaiZhao/" + XunJianDataManager.Instance.chooseXunJian.id);
  52. }
  53. if (!Directory.Exists(Application.persistentDataPath + "/PaiZhao/" + XunJianDataManager.Instance.chooseXunJian.id + "/" + XunJianDataManager.Instance.chooseXunJian.nowIndex))
  54. {
  55. Directory.CreateDirectory(Application.persistentDataPath + "/PaiZhao/" + XunJianDataManager.Instance.chooseXunJian.id + "/" + XunJianDataManager.Instance.chooseXunJian.nowIndex);
  56. }
  57. //如果此文件存在则打开
  58. //sw = file .Append();
  59. //如果此文件不存在则创建
  60. sw = file.Create();
  61. //以行的形式写入信息
  62. //sw.WriteLine(info);
  63. sw.Write(bts, 0, bts.Length);
  64. sw.Close();
  65. sw.Dispose();
  66. paizhaoData data = new paizhaoData();
  67. data.url = path;
  68. paizhaoList.Add(data);
  69. PaiZhaoManager.Instance.addVidew(data);
  70. }
  71. catch
  72. {
  73. JsonData data = new JsonData();
  74. data["type"] = "fileError";
  75. List<string> backTip = new List<string>();
  76. backTip.Add(data.ToJson());
  77. backTip.Add(data.ToJson());
  78. backTip.Add(data.ToJson());
  79. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("提示", "采集失败"+ path, Color.white, "icon", backTip, false, "", 5, "知道了.", "", "").ToJson());
  80. }
  81. // screenShot.LoadRawTextureData(bts);
  82. // screenShot.Apply();
  83. // tex.SetPixels32(req.GetData<Color32>().ToArray());
  84. // img.texture = screenShot;
  85. }
  86. else
  87. {
  88. Debug.LogError("Error AsyncGPUReadbackRequest.hasError");
  89. }
  90. }
  91. public List<paizhaoData> updateFile()
  92. {
  93. if (XunJianDataManager.Instance!=null&& XunJianDataManager.Instance.chooseXunJian!=null)
  94. {
  95. paizhaoList.Clear();
  96. if (Directory.Exists(Application.persistentDataPath + "/PaiZhao/" + XunJianDataManager.Instance.chooseXunJian.id + "/" + XunJianDataManager.Instance.chooseXunJian.nowIndex))
  97. {
  98. string[] files = Directory.GetFiles(Application.persistentDataPath + "/PaiZhao/" + XunJianDataManager.Instance.chooseXunJian.id + "/" + XunJianDataManager.Instance.chooseXunJian.nowIndex);
  99. for (int i = 0; i < files.Length; i++)
  100. {
  101. paizhaoData data = new paizhaoData();
  102. data.url = files[i];
  103. paizhaoList.Add(data);
  104. }
  105. }
  106. }
  107. return paizhaoList;
  108. }
  109. public class paizhaoData
  110. {
  111. public string url;
  112. }
  113. }