반응형

Note that 

on some platforms it is not possible to directly access the StreamingAssets folder because there is no file system access in the web platforms, and because it is compressed into the .apk file on Android. 








Reading file from StreamingAssets, Android

Save

  1. public void SaveLevel(string name)
  2. {
  3. BinaryFormatter formatter = new BinaryFormatter();
  4. using (FileStream fs = new FileStream(Path.Combine(Application.streamingAssetsPath, name), FileMode.Create))
  5. {
  6. formatter.Serialize(fs, this);
  7. }
  8. }

Load

  1. public void LoadLevel(string name)
  2. {
  3. string path = Path.Combine(Application.streamingAssetsPath, name);
  4. byte[] FileBytes;
  5. if (path.Contains("://"))
  6. {
  7. WWW www = new WWW(path);
  8. FileBytes = www.bytes;
  9. }
  10. else
  11. FileBytes = File.ReadAllBytes(path);
  12. MemoryStream MS = new MemoryStream(FileBytes);
  13. BinaryFormatter formatter = new BinaryFormatter();
  14. LevelInfo LI = (LevelInfo)formatter.Deserialize(MS);
  15. SetLevel(LI);
  16. }

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"

1 Reply

 · Add your reply
avatar image
1

Answer by HenryStrattonFW 

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 streaming assets? could they not be in resources instead? and then loaded via Resources.Load instead of manually reading the files yourself?

avatar imageVol4ica  HenryStrattonFW · Feb-04 PM 11:58 0

StreamingAssets 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.




sdf



Application.streamingAssetsPath

public static string streamingAssetsPath;

Description

Contains the path to the StreamingAssets folder (Read Only).

If you have a "StreamingAssets" folder in the Assets folder of your project, it will be copied to your player builds and be present in the path given by Application.streamingAssetsPath.

Note that on some platforms it is not possible to directly access the StreamingAssets folder because there is no file system access in the web platforms, and because it is compressed into the .apk file on Android. On those platforms, a url will be returned, which can be used using the UnityWebRequest class.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile"); public string result = ""; IEnumerator Example() { if (filePath.Contains("://")) { Networking.UnityWebRequest www = Networking.UnityWebRequest.Get(filePath); yield return www.SendWebRequest(); result = www.downloadHandler.text; } else result = System.IO.File.ReadAllText(filePath); } }


ref :

http://answers.unity3d.com/questions/1308831/reading-file-from-streamingassets-android.html

https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html


반응형

+ Recent posts