Search Unity

No file in "Application.streamingAssetsPath"

Discussion in 'Scripting' started by Klik303, Dec 10, 2017.

  1. Klik303

    Klik303

    Joined:
    Dec 10, 2016
    Posts:
    19
    Hi.
    I'm trying load a data from xml file using this code:
    File is stored in "StreamingAssets".
    Code (CSharp):
    1. string result = "";
    2.         if (Application.platform == RuntimePlatform.Android)
    3.         {
    4.            
    5.             string RootFolder = Application.streamingAssetsPath + "/levels_data.xml";
    6.             if (!File.Exists(RootFolder)) { Debug.Log("No file"); }
    7.             if (RootFolder.Contains("://"))
    8.             {
    9.                 WWW reader = new WWW(RootFolder);
    10.                 while (!reader.isDone) { };
    11.                 result = reader.text;
    12.                 Debug.Log(result);
    13.             }
    14.             else
    15.             {
    16.                 result = File.ReadAllText(RootFolder);
    17.                 Debug.Log("OK "+result);
    18.             }
    19.         }
    I don't know is it a proper code but it works. Only one thing is interesting.
    When I do a test "File.Exists" it says there is no file, but rest of code works.
    So how it works if there is no file?
    Or, if there is a file why it says "No file"?

    Thank you.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
  3. Klik303

    Klik303

    Joined:
    Dec 10, 2016
    Posts:
    19
    OK. So how to check if there is "file" represented by URL?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  5. Klik303

    Klik303

    Joined:
    Dec 10, 2016
    Posts:
    19
    Code (CSharp):
    1.  
    2. WWW reader = new WWW(RootFolder);
    3.                 while (!reader.isDone) { };
    4.                 if (!string.IsNullOrEmpty(reader.error))
    5.                 {
    6.                     Debug.Log("No file "+reader.error);
    7.                 }
    8.                 result = reader.text;
    9.                 Debug.Log(result);
    I try to check "IsNullOrEmpty" and it works but before this unity throw a error anyway:
    java.io.FileNotFoundException.

    This error is throwing in "www" constructor so how to check file before constructor "www" starts?
    Using try-catch is only solution?

    Regards.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    You should be yielding your www https://docs.unity3d.com/ScriptReference/WWW.html

    Basically, do your www call, yield it, then after the yield do the error check. And of course, this code has to be in a coroutine. The yield tells the code to wait till it's done.

    File.Exists should be enough to check if a file is there before you try to load it.
     
  7. Klik303

    Klik303

    Joined:
    Dec 10, 2016
    Posts:
    19
    OK. I though I can do the same using just while loop.

    Thank you.
     
    Last edited: Dec 11, 2017