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