Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Vol4ica · Feb 04, 2017 at 07:30 PM · androidfiles

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"

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vol4ica · Feb 04, 2017 at 11:20 PM 0
Share

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

avatar image HenryStrattonFW Vol4ica · Feb 04, 2017 at 11:36 PM 0
Share

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?

avatar image Vol4ica HenryStrattonFW · Feb 04, 2017 at 11:58 PM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Directory.GetFiles() not returning anything in Android build 0 Answers

Can't get UnityWebRequest to work on android 0 Answers

How to access android phone files programmatically? 0 Answers

Android API 29 and 30 move files from persistentDataPath 0 Answers

Is there a way to save files in a different folder than Application.persistentDataPath on Android platform? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges