- Home /
How to get Resources filepath on Android device?
Hello guys,
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:
filePath = "Assets/Resources/" + value.Enemy + ".dat"; Output is: filePath = "Assets/Resources/"CliffSide".dat";
But now I need to release this game on Android device and it is not working. How should I get the path of the asset from the android build?
Please help.
Answer by LK84 · Aug 18, 2016 at 04:44 PM
The Resoures folder simply doens't exist any more afer your build. The assets in the Resources folder get packed into the game's archive for assets.
Ok, thank you very much for your answer. So what is the file path on Android after the build?
No, I tried that. Strea$$anonymous$$g Assets not working. Documentation is not explained well, too. Do I have to rename the resources folder or anything like that?
I also looked at this, bud it does not say much: http://docs.unity3d.com/ScriptReference/Application-dataPath.html
Ok, according to Snip2Code site, Android only use WWW to read file. So in my case this worked just fine :-D
$$anonymous$$y final code:
string tempPath = System.IO.Path.Combine(Application.strea$$anonymous$$gAssetsPath, "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);
Ah ok. In your question you said you don't want to load from the resource folder ... so I didn't answer your question. You could have done the same thing with the Resources folder. You just have to rename your files to "*.bytes" and then the following should work:
// This is for multiple files.
TexAssets[] l_assets = Resources.LoadAll("nameOfResourcesSubFolder", typeof(TextAsset)).Cast<TextAsset>().ToArray();
for (int i = 0; i < l_assets .Length; i++)
{
File.WriteAllBytes(Application.persistenDataPath + "/db/" + l_assets[i].name + ".dat", l_assets[i].bytes);
}
YOU ARE $$anonymous$$Y HERO!!! THAN$$anonymous$$S A LOT!!!
Answer by shadyshrif · Jul 04, 2017 at 07:59 PM
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
Your answer
Follow this Question
Related Questions
Android build error. Failed to re-package resources. 3 Answers
CommandInvokationFailure: Failed to re-package resources. 0 Answers
How to read file on android built from Unity? 1 Answer
What's wrong with my script? 1 Answer
iPhone build doesn't copy existing files in Application.dataPath or .persistantDataPath?! 0 Answers