Reading file from StreamingAssets, Android
Save
public void SaveLevel(string name)
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream fs = new FileStream(Path.Combine(Application.streamingAssetsPath, name), FileMode.Create))
{
formatter.Serialize(fs, this);
}
}
Load
public void LoadLevel(string name)
{
string path = Path.Combine(Application.streamingAssetsPath, name);
byte[] FileBytes;
if (path.Contains("://"))
{
WWW www = new WWW(path);
FileBytes = www.bytes;
}
else
FileBytes = File.ReadAllBytes(path);
MemoryStream MS = new MemoryStream(FileBytes);
BinaryFormatter formatter = new BinaryFormatter();
LevelInfo LI = (LevelInfo)formatter.Deserialize(MS);
SetLevel(LI);
}
So what do I do. I open my own written level editor on Unity, create some levels that gonna be custom afterwards, and save them to StreamingAssets (I serialize a class that consists of arrays and some small vars). Then I build the game and access those files whenever I need. Everything works fine on PC, but nothing happens on Android platform. I checked File.Exists(path), and it returned false (however I'm not sure if it works correctly on Android). Path is "jar:file:///mnt/asec/com.Vol4icaGames.WR-1/pkg.apk!/assets/TestLevel"
Answer by HenryStrattonFW · Feb 04, 2017 at 11:08 PM
Some platforms like android do not give you direct access to the streaming assets directory. On android the streaming assets directory is compressed into the apk, and so is not available for you to just read/write files to normally.
https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html
It is advised that if you want to save/load files on mobile devices, you should be doing so to the persistantDataPath instead, as you should have no problem reading/writing files to this directory.
That's true. But persistantDataPath is for saving some data after the build and reading it afterwards. What I want to do is to save information about game levels themselves, not the player progress in them. This means that I have to have pre-written info files that I have to be able to read when I want to load level. Even if I manage to copy level files into persistantDataPath, somehow, it is located on SD-card (is it?) and if user has no such card (inserted or card slot at all) the game gonna break... That's what I don't want. I have no idea how to store level info in other way.. To create hundreds of similar scenes is not what I would want to do..
Ah sorry I missread part of the initial question. But now that I have more of an idea of what you are trying, is there any specific reason that these level files you're saving at edit time need to be in strea$$anonymous$$g assets? could they not be in resources ins$$anonymous$$d? and then loaded via Resources.Load ins$$anonymous$$d of manually reading the files yourself?
Strea$$anonymous$$gAssets is a folder that keeps files in it in the form they were created. Not compressed, not converted into some other format etc. So I can access any file by its name. Can I do same thing with Resources folder? I know how to do it with sprites etc, but have no idea what to do with byte file.
Answer by zoranigic · Nov 29, 2019 at 10:05 AM
It is not possible to access the StreamingAssets folder on WebGL and Android platforms. No file access is available on WebGL. Android uses a compressed .apk file. These platforms return a URL. Use the UnityWebRequest class to access the Assets.