BaoGaoUpLoadManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. public void initBG()
  16. {
  17. for (int i = 0;i<itemlist.Count;i++)
  18. {
  19. Destroy(itemlist[i].gameObject);
  20. }
  21. itemlist.Clear();
  22. Debug.Log("BaoGaoWindowManager.Instance.XQData.workOrder.reports.paperReports==>");
  23. if(BaoGaoWindowManager.Instance.XQData.workOrder.reports!=null&&BaoGaoWindowManager.Instance.XQData.workOrder.reports.paperReports!=null)
  24. {
  25. Debug.Log("BaoGaoWindowManager.Instance.XQData.workOrder.reports.paperReports==>"+BaoGaoWindowManager.Instance.XQData.workOrder.reports.paperReports.Count);
  26. for (int i = 0;i< BaoGaoWindowManager.Instance.XQData.workOrder.reports.paperReports.Count;i++)
  27. {
  28. downloadimg(BaoGaoWindowManager.Instance.XQData.workOrder.reports.paperReports[i]);
  29. }
  30. }
  31. }
  32. public void downloadimg(string path)
  33. {
  34. Debug.Log("开始下载图片===》"+path);
  35. UpLoadmageItem nitem = GameObject.Instantiate(item,item.transform.parent);
  36. nitem.GetComponent<ImageLoadManager>().loadimage(path);
  37. nitem.gameObject.SetActive(true);
  38. nitem.path = path;
  39. itemlist.Add(nitem);
  40. }
  41. string imgpath="";
  42. public UpLoadmageItem item;
  43. public List<UpLoadmageItem> itemlist;
  44. public void showImg()
  45. {
  46. if( itemlist.Count>=5)
  47. {
  48. TianJiWindowManager.Instance.showPop(4,"最多上传5张图");
  49. Debug.Log("最多上传5张图");
  50. return;
  51. }
  52. NativeGallery.GetImageFromGallery((string path) => {
  53. Debug.Log("Image path: " + path);
  54. imgpath = path;
  55. });
  56. }
  57. void Update()
  58. {
  59. if(imgpath!="")
  60. {
  61. Texture2D t2d = LoadTextureFromPath(imgpath);
  62. UpLoadmageItem nitem = GameObject.Instantiate(item,item.transform.parent);
  63. nitem.gameObject.SetActive(true);
  64. nitem.init(t2d,imgpath);
  65. itemlist.Add(nitem);
  66. imgpath="";
  67. }
  68. }
  69. public void gotocompleteGongdan(bool isDraft)
  70. {
  71. StartCoroutine(uploadfile(isDraft));
  72. }
  73. IEnumerator uploadfile(bool isDraft)
  74. {
  75. JsonData data= new JsonData();
  76. data["workOrderId"] =int.Parse(ChuLiItemManager.ChooseData["id"].ToString()) ;
  77. data["reportsType"] = 1;
  78. data["isDraft"] = isDraft;
  79. data["paperReports"] = new JsonData();
  80. for (int i=0;i<itemlist.Count;i++)
  81. {
  82. data["paperReports"].Add(itemlist[i].path);
  83. }
  84. if( itemlist.Count<=0&&!isDraft)
  85. {
  86. TianJiWindowManager.Instance.showPop(4,"请上传图片");
  87. Debug.Log("请上传图片");
  88. }
  89. else
  90. {
  91. int ct =0;
  92. TianJiWindowManager.Instance.showPop(7,"");
  93. for (int i=0;i<itemlist.Count;i++)
  94. {
  95. UpLoadmageItem n = itemlist[i].GetComponent<UpLoadmageItem>();
  96. if(itemlist[i].fildData==null)
  97. {
  98. ct++;
  99. }
  100. else
  101. {
  102. itemlist[i].startUpLoad((b)=>{
  103. ct++;
  104. });
  105. }
  106. }
  107. while(itemlist.Count!=ct)
  108. {
  109. yield return null;
  110. }
  111. DataManager.Instance.completeGongdan(data,(str)=>{
  112. TianJiWindowManager.Instance.closepop();
  113. Debug.Log("completeGongdancompleteGongdan====>"+str);
  114. QuanJuBackManager.Instance.back();
  115. });
  116. }
  117. }
  118. public Texture2D LoadTextureFromPath(string imagePath)
  119. {
  120. // 检查文件是否存在
  121. if (!System.IO.File.Exists(imagePath))
  122. {
  123. Debug.LogError("File not found at path: " + imagePath);
  124. return null;
  125. }
  126. // 读取文件字节
  127. byte[] fileData = System.IO.File.ReadAllBytes(imagePath);
  128. // 创建新的Texture2D
  129. Texture2D tex = new Texture2D(2, 2);
  130. // 加载图片数据到Texture2D
  131. if (tex.LoadImage(fileData)) // 这会自动调整Texture2D的大小
  132. {
  133. return tex;
  134. }
  135. return null;
  136. }
  137. }