ios中while()和 android中的不同之处

 

ios中while()不能www方式读取文件

void FDJson()
{
   using(WWW _load = new WWW("file://"+FileFunc.GetStreamPathRW("FontPreferenceC.json")))  //
   {
       while(!_load.isDone)
       {}
  }
}

安卓却可以这样读取

 

经过修改

IEnumerator FDJson()
{
    using(WWW _load = new WWW("file://"+FileFunc.GetStreamPathRW("FontPreferenceC.json")))  //
    {
        while(!_load.isDone)
        {
          yield return null;
        } 
    }
}    

 

可以在ios下正常读取了

 

 

但是这个要用到IEnumerator 枚举器,很不方便,所以读取文件最好用system.io接口读取

public static string ReadTextFromFile (string path)
    {
        if (System.IO.File.Exists (path))
        {
            return System.IO.File.ReadAllText (path, System.Text.Encoding.UTF8);
        }
        else return null;
    }

 

但io在安卓中不适用,因为安卓的文件都是jar压缩格式,所以在安卓中要用www读取文件,

http://answers.unity3d.com/questions/210909/android-streamingassets-file-access.html

So I was never able to figure out if it is possible to use the System.IO.FileInfo class to check the existence of a file that is added to your Android .APK file through the StreamingAssets directory. At this point I really doubt it's possible at all. The reason for this is that the .APK file is an archive, as you may know you can open a .APK file using any archiving utility (WinRAR for example) to inspect it's contents as I touched on above. So the FileInfo class apparently does not support archives like this.

Fortunately the WWW class does support a URI in the style of a JAR URL. So instead I am just pointing my WWW object at the location where the file might be (using the correct JAR URL syntax) and seeing if I come back with an error.

This forums post was extremely insightful in showing how exactly to open a file from the StreamingAssets folder. I'm kind of surprised that this issue isn't better documented, but hopefully in the future this post will save someone a lot of time.

In review:

  1. Don't use System.IO.FileInfo to access files that are archived in your .APK
  2. WWW is your best friend.
  3. The correct URI to access StreamingAssets on Android devices is: "jar:file://" + Application.dataPath + "!/assets/" + fileName

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值