BaoGaoUpLoadManager.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using static DownLoadXRManager;
  8. public class BaoGaoUpLoadManager : MonoBehaviour
  9. {
  10. public static BaoGaoUpLoadManager Instance;
  11. void Awake()
  12. {
  13. Instance = this;
  14. }
  15. void OnEnable()
  16. {
  17. initBG(BaoGaoWindowManager.Instance.XQData);
  18. }
  19. public void initBG(BaoBiaoDetail data)
  20. {
  21. for (int i = 0;i< data.workOrder.reports.paperReports.Count;i++)
  22. {
  23. downloadimg(data.workOrder.reports.paperReports[i]);
  24. }
  25. }
  26. public void downloadimg(string path)
  27. {
  28. Debug.Log("开始下载图片===》"+path);
  29. DownLoadResources dlr = new DownLoadResources();
  30. dlr.path = path;
  31. // 文件上传
  32. DownLoadXRManager.DownLoadForTexture(dlr, msg =>
  33. {
  34. Texture2D t2d = msg;
  35. UpLoadmageItem nitem = GameObject.Instantiate(item,item.transform.parent);
  36. nitem.gameObject.SetActive(true);
  37. nitem.init(t2d,imgpath);
  38. itemlist.Add(nitem);
  39. },(f)=>{
  40. });
  41. }
  42. string imgpath="";
  43. public UpLoadmageItem item;
  44. public List<UpLoadmageItem> itemlist;
  45. public void showImg()
  46. {
  47. if( itemlist.Count>=5)
  48. {
  49. TianJiWindowManager.Instance.showPop(4,"最多上传5张图");
  50. Debug.Log("最多上传5张图");
  51. return;
  52. }
  53. NativeGallery.GetImageFromGallery((string path) => {
  54. Debug.Log("Image path: " + path);
  55. imgpath = path;
  56. });
  57. }
  58. void Update()
  59. {
  60. if(imgpath!="")
  61. {
  62. Texture2D t2d = LoadTextureFromPath(imgpath);
  63. UpLoadmageItem nitem = GameObject.Instantiate(item,item.transform.parent);
  64. nitem.gameObject.SetActive(true);
  65. nitem.init(t2d,imgpath);
  66. itemlist.Add(nitem);
  67. imgpath="";
  68. }
  69. }
  70. public void gotocompleteGongdan(bool isDraft)
  71. {
  72. JsonData data= new JsonData();
  73. data["workOrderId"] = BaoGaoWindowManager.Instance.XQData.workOrder.id;
  74. data["reportsType"] = 2;
  75. data["isDraft"] = isDraft;
  76. data["paperReports"] = new JsonData();
  77. for (int i=0;i<itemlist.Count;i++)
  78. {
  79. data["paperReports"].Add(itemlist[i].path);
  80. }
  81. if( data["paperReports"].Count<=0)
  82. {
  83. TianJiWindowManager.Instance.showPop(4,"请上传图片");
  84. Debug.Log("请上传图片");
  85. return;
  86. }
  87. DataManager.Instance.completeGongdan(data,(str)=>{
  88. Debug.Log("completeGongdancompleteGongdan====>"+str);
  89. QuanJuBackManager.Instance.back();
  90. });
  91. }
  92. public Texture2D LoadTextureFromPath(string imagePath)
  93. {
  94. // 检查文件是否存在
  95. if (!System.IO.File.Exists(imagePath))
  96. {
  97. Debug.LogError("File not found at path: " + imagePath);
  98. return null;
  99. }
  100. // 读取文件字节
  101. byte[] fileData = System.IO.File.ReadAllBytes(imagePath);
  102. // 创建新的Texture2D
  103. Texture2D tex = new Texture2D(2, 2);
  104. // 加载图片数据到Texture2D
  105. if (tex.LoadImage(fileData)) // 这会自动调整Texture2D的大小
  106. {
  107. return tex;
  108. }
  109. return null;
  110. }
  111. }