I went through some questions in the forum and none of the answers was relative to my question or it did not work.
In my project I have a Resources folder. I am not loading files from this folder, I only need to get the path of the file in this folder. In the Editor I have this and it is working perfectly:
Ok, according to [Snip2Code][1] site, Android only use WWW to read file. So in my case this worked just fine :-D My final code: string tempPath = System.IO.Path.Combine(Application.streamingAssetsPath, "CliffSide.dat"); // Android only use WWW to read file WWW reader = new WWW(tempPath); while ( ! reader.isDone) {} filePath = Application.persistentDataPath + "/db"; System.IO.File.WriteAllBytes(filePath, reader.bytes); [1]: https://www.snip2code.com/Snippet/14142/Get-Unity-StreamingAssets-file-path-with
To solve this problem I created pre-compilation script which will run before the compilation; this script will list the names of the files you want to access later in text file
#if UNITY_EDITOR
using UnityEditor.Build;
using UnityEditor;
using System.IO;
public class BM_AndroidBuildPrepartion : IPreprocessBuild
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildTarget target, string path)
{
// Do the preprocessing here
string[] fileEntries = Directory.GetFiles("Assets/Resources/Prefabs/alphabet", "*.prefab");
System.IO.Directory.CreateDirectory("Assets/StreamingAssets/");
using (StreamWriter sw = new StreamWriter("Assets/StreamingAssets/alphabet.txt", false))
{
foreach (string filename in fileEntries) {
sw.WriteLine(Path.GetFileNameWithoutExtension(filename));
}
}
}
}
#endif
then I read the text file and you can access the files in same way but you have to put them inside your project in Assets/StreamingAssets/
#if UNITY_ANDROID
string path = "jar:file://" + Application.dataPath + "!/assets/alphabet.txt";
WWW wwwfile = new WWW(path);
while (!wwwfile.isDone) { }
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, "alphabet.t");
File.WriteAllBytes(filepath, wwwfile.bytes);
StreamReader wr = new StreamReader(filepath);
string line;
while ((line = wr.ReadLine()) != null)
{
//your code
}
#endif
1 Like
Should I see the file/directory in the build directory? I don't see any directory in the app folder
Ok, thank you very much for your answer. So what is the file path on Android after the build?
– CodabyOk, according to [Snip2Code][1] site, Android only use WWW to read file. So in my case this worked just fine :-D My final code: string tempPath = System.IO.Path.Combine(Application.streamingAssetsPath, "CliffSide.dat"); // Android only use WWW to read file WWW reader = new WWW(tempPath); while ( ! reader.isDone) {} filePath = Application.persistentDataPath + "/db"; System.IO.File.WriteAllBytes(filePath, reader.bytes); [1]: https://www.snip2code.com/Snippet/14142/Get-Unity-StreamingAssets-file-path-with
– Codaby