- Home /
StreamingAssets - reading from file on android
this is not a dupe - but the closest thing i could find is https://answers.unity.com/questions/1308831/reading-file-from-streamingassets-android.html
I get that the optimal way to do data is during runtime - and i also specifically read multiple places that resources.load() is not the way to go and to use streamingassets, so thats the road i go down, also because i want to keep a very specific file structure.
The reason: currently in there's going to be, a -lot- of individual files with specific naming conventions that i need to be able to read through and pick out, and get specific data, also to help remove the entire need for loading the data in, as that would not be viable, with 100k files. And writing in these data hardcoded in is also not viable, for obvious reasons.
Here's what im currently doing, which may be wrong. This is my first android built unity game.
the start of my data script where i determine platform and path.
cObject = GameObject.Find("CoreScripts");
cScript = cObject.GetComponent<CoreScript>();
Debug.Log("Started Data Script");
if (Application.platform == RuntimePlatform.Android)
{
currentPlatform = "android";
toDataPath = "jar:file://" + Application.dataPath + "!/assets/Data/";
cScript.SetBodyTextDisplay("UITextBox", "Canvas", "Currently using Android build");
}
else if (Application.platform == RuntimePlatform.WindowsPlayer)
{
currentPlatform = "windows";
toDataPath = Application.streamingAssetsPath + "/Data/";
cScript.SetBodyTextDisplay("UITextBox", "Canvas", "Currently using Windows build");
}
else if (Application.platform == RuntimePlatform.WindowsEditor)
{
currentPlatform = "editor";
toDataPath = Application.streamingAssetsPath + "/Data/";
cScript.SetBodyTextDisplay("UITextBox", "Canvas", "Currently using Editor build");
}
here's where i, attempt, to read the data, which currently does not seem to function.
public IEnumerator ReadData(string pathTo, string fileName) {
string tempData = "null";
fileName = fileName + dataExtension;
cScript.SetBodyTextDisplay("UITextBox", "Canvas", "Test1: "+Application.streamingAssetsPath);
cScript.SetBodyTextDisplay("UITextBox", "Canvas", "Test2: "+pathTo + fileName);
if (currentPlatform.Equals("android"))
{
WWW read = new WWW(pathTo+fileName);
while (!read.isDone)
{
yield return null;
}
tempData = read.text;
read.Dispose();
cScript.SetBodyTextDisplay("UITextBox", "Canvas", "Reading: " + pathTo + fileName + " - " + tempData);
}
else
{
StreamReader read = new StreamReader(pathTo + fileName);
tempData = read.ReadLine();
cScript.SetBodyTextDisplay("UITextBox", "Canvas", "Reading: " + pathTo + fileName + " - " + tempData);
Debug.Log("Reading: " + pathTo + fileName + " - " + tempData);
read.Close();
}
//hmm, return string? think... tempData
yield return null;
}
I am aware of the access issue, but i should none the less be able to read from said file, correct? (also, i am doing a coroutine for said ienumerator)
What exactly can i do about this? Because i've been reading into it, trying different things, and varius different paths, none seem to work (i have seen some people use path.combine syntax which i wasnt sure might be the issue. I am also defering directly to the unity
https://docs.unity3d.com/Manual/StreamingAssets.html
In terms of finding the correct path. As you can see in the examples above.
Also i find it incredibly tedious in terms of saving/storing data on android? Is it just me or does it seem like there's absolutely 0 good ways to do any data at all properly on said device?
Also note, everything does work on a normal windows build.
Your answer
Follow this Question
Related Questions
How to read xml file on Android using www and StreamingAssets? 2 Answers
Loading an image from streamingassets fails every time on Android 1 Answer
I can't figure out how to use www to copy a file / very confused about porting to android... 0 Answers
how do you correctly copy a jpg file from streaming assets folder using www? 1 Answer