当前位置:   article > 正文

Unity(十六) Unity加载本地/网络图片_unity rawimage 网络图片

unity rawimage 网络图片

更新日期:2021.1.13

工具:Unity2019.4.10

读取手机内存里的图片

"/storage/emulated/0" 是Android内存主目录

注意:file://+路径,总共是3个斜杠

  1. //方式1:使用www加载
  2. IEnumerator wwwLoadTexture(string filePath)
  3. {
  4. //例如 "file:///storage/emulated/0/Download/123.jpg"
  5. WWW www = new WWW(filePath);
  6. yield return www;
  7. if (www.isDone && www.error == null)
  8. {
  9. Texture2D texture = www.texture;
  10. //rawImage.texture = texture;
  11. }
  12. }
  13. //方式2:使用UnityWebRequest加载
  14. IEnumerator UnityWebRequestLoadPic(string dirpath)
  15. {
  16. //例如 "file:///storage/emulated/0/Download/123.jpg";
  17. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(dirpath))
  18. {
  19. yield return request.SendWebRequest();
  20. if (request.isHttpError || request.isNetworkError)
  21. {
  22. Debug.LogError("request error");
  23. }
  24. else
  25. {
  26. var texture = DownloadHandlerTexture.GetContent(request);
  27. //rawImage.texture = texture;
  28. }
  29. }
  30. }

读取StreamingAssets目录

  1. public static IEnumerator load()
  2. {
  3. string path = string.Empty;
  4. string line1 = string.Empty;
  5. //Window路径
  6. path = Application.streamingAssetsPath + "/aaa.xml"; //方法1 读取streamingAssetsPath目录
  7. //path = "jar:file://" + Application.dataPath + "!/assets/" + "aaa.xml";//方法2和方法1一样
  8. //PC路径
  9. path = "file://" + Application.streamingAssetsPath + "/aaa.xml";
  10. //或 path = Application.dataPath + "/StreamingAssets" + "/aaa.xml";
  11. WWW wWA = new WWW(path);///WWW读取在各个平台上都可使用
  12. yield return wWA;
  13. line1 = wWA.text;
  14. Debug.Log(line1);
  15. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/103524
推荐阅读
相关标签
  

闽ICP备14008679号