InfoWindow.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using RenderHeads.Media.AVProVideo;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. using UnityEngine.UI;
  8. public class InfoWindow : MonoBehaviour
  9. {
  10. public Text titlestr;
  11. public Text infoStr;
  12. public Text infoStr2;
  13. public RawImage img;
  14. public RawImage img2;
  15. public GameObject mp4;
  16. public MediaPlayer mp;
  17. public void init (string info,string url,string info2,int index,string title,string application)
  18. {
  19. StopAllCoroutines();
  20. idx=0;
  21. mp.Stop();
  22. titlestr.text = TabListManager.Instance.GetTextByKey(title);
  23. infoStr.text = TabListManager.Instance.GetTextByKey(application);
  24. infoStr2.text = TabListManager.Instance.GetTextByKey(info)+"\n\n\n"+TabListManager.Instance.GetTextByKey(info2);
  25. if(url.Contains("mp4"))
  26. {
  27. mp4.SetActive(true);
  28. img2.gameObject.SetActive(false);
  29. mp.OpenMedia(MediaPathType.RelativeToStreamingAssetsFolder,url,true);
  30. }
  31. else
  32. {
  33. mp4.SetActive(false);
  34. img2.gameObject.SetActive(true);
  35. }
  36. // 构建文件路径
  37. filePath = Application.streamingAssetsPath+"/DeMa/png/"+index;
  38. img.gameObject.SetActive(true);
  39. Debug.Log("文件路径==》"+filePath);
  40. #if UNITY_EDITOR
  41. string fileListPath = Path.Combine(Application.streamingAssetsPath, "DeMa/png/"+index);
  42. // 获取 StreamingAssets 文件夹中的所有文件
  43. files= Directory.GetFiles(fileListPath, "*.png"); // 假设图片是 PNG 格式
  44. if(files.Length<=0)
  45. {
  46. img.gameObject.SetActive(false);
  47. }
  48. else
  49. {
  50. StartCoroutine(LoadImages());
  51. }
  52. Debug.Log("文件路径llllll==》"+files.Length);
  53. #else
  54. StartCoroutine(loadImg());
  55. #endif
  56. }
  57. string filePath ;
  58. string[] files ;
  59. int idx=0;
  60. private void OnDisable() {
  61. // infoStr2.transform.parent.gameObject.SetActive(false);
  62. }
  63. Texture2D texture ;
  64. IEnumerator loadImg()
  65. {
  66. // 构建文件路径
  67. string f = filePath+"/"+idx+".png";
  68. Debug.Log(" to load image: " + f);
  69. // 使用 UnityWebRequest 加载图片
  70. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(f))
  71. {
  72. yield return request.SendWebRequest();
  73. if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
  74. {
  75. if(idx==0)
  76. {
  77. img.gameObject.SetActive(false);
  78. }
  79. else
  80. {
  81. idx=0;
  82. StartCoroutine(loadImg());
  83. }
  84. Debug.LogError("Failed to load image: " + request.error);
  85. }
  86. else
  87. {
  88. img.gameObject.SetActive(true);
  89. if(texture!=null)
  90. {
  91. Destroy(texture);
  92. }
  93. // 获取 Texture2D
  94. texture = DownloadHandlerTexture.GetContent(request);
  95. // 显示图片
  96. img.texture = texture;
  97. // 等待一段时间再加载下一张图片
  98. yield return new WaitForSeconds(3.0f);
  99. idx++;
  100. StartCoroutine(loadImg());
  101. }
  102. }
  103. }
  104. IEnumerator LoadImages()
  105. {
  106. // 构建文件路径
  107. string filePath = files[idx];
  108. if(texture!=null)
  109. {
  110. Destroy(texture);
  111. }
  112. // 读取文件字节数据
  113. byte[] fileData = File.ReadAllBytes(filePath);
  114. // 创建 Texture2D 并加载图片数据
  115. texture = new Texture2D(2, 2);
  116. texture.LoadImage(fileData);
  117. // 显示图片
  118. img.texture = texture;
  119. // 等待一段时间再加载下一张图片
  120. yield return new WaitForSeconds(3.0f);
  121. idx++;
  122. if(idx>=files.Length)
  123. {
  124. idx=0;
  125. }
  126. StartCoroutine(LoadImages());
  127. }
  128. }