title: Access Unity StreamingAssets on Android
body: <p>I wrote some code to get all the folders inside a folder inside the StreamingAssets and then to get all the files in that folder.
  It works well on Windows but I can't get it to work on Android. </p>
  
  <p>Here is the code :</p>
  
  <pre><code>foreach (string s in Directory.GetDirectories(model.path + "/Levels")) {
          GameObject el = (GameObject)Instantiate(listButton, Vector3.zero, Quaternion.identity);
          el.transform.SetParent(grid1.transform);
          string[] words = s.Split('/');
          #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
                              words = s.Split('\\');
          #endif
          el.GetComponentInChildren&lt;Text&gt;().text = words[words.Length - 1];
          el.GetComponent&lt;Button&gt;().onClick.AddListener(() =&gt; {
              MapSizeButtonClick(Int32.Parse(el.GetComponentInChildren&lt;Text&gt;().text));
          });
      }
  </code></pre>
  
  <p>I had to add the #if UNITY_STANDALONE_WIN for windows because of the '\'.</p>
  
  <p>Then for the files inside that folder I wrote this :</p>
  
  <pre><code>foreach (string s in Directory.GetFiles(model.path + "/Levels/" + model.mapSize + "/" + model.colorNumber)) {
          string[] words = s.Split('/');
          #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
                      words = s.Split('\\');
          #endif
          words = words[words.Length - 1].Split('.');
          if (words[1] == "json") {
              GameObject el = (GameObject)Instantiate(listButton, Vector3.zero, Quaternion.identity);
              el.transform.SetParent(grid3.transform);
              el.GetComponentInChildren&lt;Text&gt;().text = words[0];
              el.GetComponent&lt;Button&gt;().onClick.AddListener(() =&gt; {
                  levelButtonClick(Int32.Parse(el.GetComponentInChildren&lt;Text&gt;().text));
              });
          }
      }
  </code></pre>
  
  <p>I'm using UnityEngine.UI.Directory but I read that it can't be use on Android and I must use WWW instead. Something like this :</p>
  
  <pre><code>string xmlSetUpPath = "jar:file://" + Application.dataPath + "!/assets/xml/xmlSetUp.xml";
  
          WWW book1WWW = new WWW(book1Path);
          yield return book1WWW;
          if(!File.Exists(Application.persistentDataPath + "/xml/book1.xml"))
          {
              File.WriteAllBytes(Application.persistentDataPath + "/xml/book1.xml", book1WWW.bytes); 
          }
  </code></pre>
  
  <p>However I couldn't find anything like Directory.getFiles() or Directory.getFolder(). So I thought I could use WWW to check if the file exists (as the file names are 1.json, 2.json, 3.json) and iterate until a file doesn't exist.</p>
  
  <p>But I would also need to check if a folder exists for the 1st part of the script and I can't find how to check if a folder exists with WWW. (My folders names are also 1,2,3,4...)</p>
  
  <p>So how to check if a folder exists with WWW ?</p>
  
  <p>Or what else can I do ?</p>
  
  <p><strong>UPDATE :</strong></p>
  
  <p>I updated the code, now it looks like this :</p>
  
  <pre><code>for (int i = 0; i &lt; 100; i++) {
          if (Directory.Exists(Path.Combine(model.path, "Levels/" + i.ToString()))){
              GameObject el = (GameObject)Instantiate(listButton, Vector3.zero, Quaternion.identity);
              el.transform.SetParent(grid1.transform);
              el.GetComponentInChildren&lt;Text&gt;().text = i.ToString();
              el.GetComponent&lt;Button&gt;().onClick.AddListener(() =&gt; {
                  MapSizeButtonClick(Int32.Parse(el.GetComponentInChildren&lt;Text&gt;().text));
              });
          }
      }
  </code></pre>
  
  <p>I use path.combine() and check every folder from 0 to 100. It works on Windows but it still doesn't work in Android.</p>
  
  <p>The path is set like this : </p>
  
  <pre><code>#if UNITY_IPHONE
          path = Application.dataPath + "/Raw";
      #endif
  
      #if UNITY_ANDROID
          path = "jar:file://" + Application.dataPath + "!/assets";
      #endif
  
      #if UNITY_STANDALONE || UNITY_EDITOR
          path = Application.dataPath + "/StreamingAssets";
      #endif
  </code></pre>
  
  <p><strong>QUESTION UPDATE</strong></p>
  
  <p>I made a script that generate the file paths. The paths are always the same : "FolderNumerotedFromYToX/FolderNumerotedFromYToX/FileNumerotedFrom1ToX.json". The generation works but the writing to JSON doesn't. It outputs "{}".</p>
  
  <pre><code>        // Generate level list
      List&lt;KeyValuePair&lt;int, List&lt;KeyValuePair&lt;int, List&lt;int&gt;&gt;&gt;&gt;&gt; l;
      l = new List&lt;KeyValuePair&lt;int, List&lt;KeyValuePair&lt;int, List&lt;int&gt;&gt;&gt;&gt;&gt;();
      for (int i = 1; i &lt; 100; i++) {
          if (Directory.Exists(Path.Combine(Path.Combine(Application.dataPath, "StreamingAssets"), "Levels/" + i.ToString()))) {
              l.Add(new KeyValuePair&lt;int, List&lt;KeyValuePair&lt;int, List&lt;int&gt;&gt;&gt;&gt;(i, new List&lt;KeyValuePair&lt;int, List&lt;int&gt;&gt;&gt;()));
              for (int j = 1; j &lt; 100; j++) {
                  if (Directory.Exists(Path.Combine(Path.Combine(Application.dataPath, "StreamingAssets"), "Levels/" + i + "/" + j))) {
                      l[l.Count - 1].Value.Add(new KeyValuePair&lt;int, List&lt;int&gt;&gt;(j, new List&lt;int&gt;()));
                      int k = 1;
                      while (true) {
                          if (File.Exists(Path.Combine(Path.Combine(Application.dataPath, "StreamingAssets"), "Levels/" + i + "/" + j + "/" + k + ".json"))) {
                              l[l.Count - 1].Value[l[l.Count - 1].Value.Count - 1].Value.Add(k);
                          }
                          else
                              break;
                          k++;
                      }
                  }
              }
          }
      }
      string ljson = JsonUtility.ToJson(l);
      var lsr = File.CreateText("Assets/StreamingAssets/levels.json");
      lsr.WriteLine(ljson);
      lsr.Close();
  </code></pre>
  
  <p>Can you help me finding a way to store this to a file ?</p>