Unity读取windows和Android SDcard文件

全部都是路径惹的祸

读取 windows D: 盘下的  test.txt 文件,

编辑器里运行,console上可以查看:

windows上 路径  / 要写成 \\  写一个也不行,可能会被转义,反正也无法读取内容

[csharp]  view plain  copy
  1. string windowsUrl = "file://D:\\test.txt";  
  2.   
  3.       WWW w = new WWW(windowsUrl);  
  4.   
  5.       yield return w;  
  6.   
  7.       if (w.isDone)  
  8.       {  
  9.   
  10.           Debug.Log("读取 D:盘 test文件 ====  "+w.text);  
  11.   
  12.       }  



Androud相关的各大论坛相关文章很多,但是说的都不太清楚,所以特此记录。

log中 我打印了 Unity里面的路径,Androd上需要注意的也是 路径上的统一:

Android上获取Api提供的路径:

[java]  view plain  copy
  1. Log.e(TAG, "onCreate: packageName = " + this.getExternalCacheDir());  
  2.   
  3.      Log.e(TAG, "onCreate: packageName = " + this.getExternalFilesDir(""));  
  4.        
  5.      Log.e(TAG, "onCreate: packageName = " + this.getCacheDir());  
  6.   
  7.      Log.e(TAG, "onCreate: packageName = " + this.getPackageName());  
  8.        
  9.      Log.e(TAG, "onCreate: packageName = " + Environment.getDataDirectory());  
  10.   
  11.      Log.e(TAG, "onCreate: packageName = " + Environment.getDataDirectory().getAbsolutePath());  
  12.        
  13.      Log.e(TAG, "onCreate: packageName = " + Environment.getExternalStorageDirectory());  



IO方式 加载sdcard上的图片资源

加载的  /storage/emulated/0/ProjectName/Image/avatar/20170523_164417.jpg,


[csharp]  view plain  copy
  1. image = this.GetComponentInChildren<Image>();  
  2.   
  3.       Debug.Log("IO加载用时: image = this.GetComponent<Image> ==========  " + image);  
  4.   
  5.       Debug.Log("IO加载:  Application.dataPath "  + Application.dataPath );  
  6.       // Application.dataPath /data/app/com.putao.ptx.core-1/base.apk  
  7.   
  8.       Debug.Log("IO加载:  Application.persistentDataPath " + Application.persistentDataPath);  
  9.       // Application.persistentDataPath /storage/emulated/0/Android/data/com.putao.ptx.core/files   
  10.   
  11.   
  12.       Debug.Log("IO加载:GameObject.Find" + GameObject.Find("Canvas/Avator").GetComponent<Image>());  
  13.   
  14.        // /data/user/0/com.putao.paichallenge/cache/20170524_130527.jpg  
  15.   
  16.   
  17.       // path =======  "/storage/emulated/0/ProjectName/Image/avatar/20170523_164417.jpg"  
  18.       FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);  
  19.       fileStream.Seek(0, SeekOrigin.Begin);  
  20.       //创建文件长度缓冲区  
  21.       byte[] bytes = new byte[fileStream.Length];  
  22.       //读取文件  
  23.       fileStream.Read(bytes, 0, (int)fileStream.Length);  
  24.       //释放文件读取流  
  25.       fileStream.Close();  
  26.       fileStream.Dispose();  
  27.       fileStream = null;  
  28.   
  29.       //创建Texture  
  30.       int width = 300;  
  31.       int height = 372;  
  32.       Texture2D texture = new Texture2D(width, height);  
  33.       texture.LoadImage(bytes);  
  34.   
  35.   
  36.       //创建Sprite       
  37.       Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width,texture.height), new Vector2(0.5f, 0.5f));  
  38.   
  39.       image.sprite = sprite;  

log上可以看出来  android上 getExternalFilesDir("") 和 Unity里面的Application.persistentDataPath是一致的

   /storage/emulated/0/Android/data/com.putao.ptx.core/files


WWW方式加载本地图片   

url=  "file://"+ "/storage/emulated/0/ProjectName/Image/avatar/20170523_164417.jpg"

根本不是加"jar:file://"。。。某些博主根本就不试试么,瞎写。

[csharp]  view plain  copy
  1. public void LoadByWWW(String path)  
  2.     {  
  3.         StartCoroutine(doLoadByWWW(path));  
  4.   
  5.     }  
  6.   
  7.     IEnumerator doLoadByWWW(String path)  
  8.     {  
  9.         string url = "file://" + "/storage/emulated/0/PaiChallenge/Image/avatar/20170523_164417.jpg";  
  10.         Debug.Log("doLoadByWWW == url ==================   " + url);  
  11.   
  12.         WWW w = new WWW(url);  
  13.   
  14.         yield return w;  
  15.   
  16.         if (w.isDone)  
  17.         {  
  18.             Sprite sprite = Sprite.Create(w.texture, new Rect(0, 0, w.texture.width, w.texture.height), new Vector2(0.5f, 0.5f));  
  19.   
  20.             GameObject.Find("Canvas/Avator").GetComponent<Image>().sprite = sprite;  
  21.   
  22.         }  


还有 就是权限问题的添加 :Player settings -- Other settings -- write permission的设置 Sdcard。这个是在Unity编辑器里打包的情况。

如果导出到studio 里面的话,可自行修改Manifest文件。


unity正版软件--官方指定代理广州元易qq微信2853068008

原文链接https://blog.csdn.net/Silk2018/article/details/72677136

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值