Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by prak · May 29, 2013 at 07:03 PM · androiddatapath

Android - Straight answer for accessing data via Application.dataPath

I've started porting my game to Android and the first thing I tried (like any good programmer) was to build and run. :) I noticed that none of the xml files (that set initial values) were loaded. I've basically traced it down to what looks like the xml serializer is not finding the files. I don't know how to know if and where my data files are getting saved when my game deploys.

I did a bunch of searching around and found all kinds of answers (some out of date, some solving other issues). I just want a clear solution for accessing data on android.

My code pre-android is such:

     public static void setDefaultFileLocation() {
         fileLocation = Application.dataPath + "\\Data";
         Debug.Log("fileLocation : " + fileLocation);
     }
 
     public static void SaveXML<T>(T data, string fileName) { 
         if (string.IsNullOrEmpty(fileLocation))
             setDefaultFileLocation();
          
         string _data = SerializeObject<T>(data); 
          
         StreamWriter writer; 
         FileInfo t = new FileInfo(fileLocation + "\\" + fileName + ".xml"); 
         if (!t.Exists) { 
             writer = t.CreateText(); 
         }
         else { 
             t.Delete();
             writer = t.CreateText(); 
         } 
         writer.Write(_data); 
         writer.Close(); 
         Debug.Log("XML File saved : " + fileName); 
     }
  
     public static T LoadXML<T>(string fileName) { 
         if (string.IsNullOrEmpty(fileLocation))
             setDefaultFileLocation();
          
         if (!File.Exists(fileLocation + "\\" + fileName + ".xml"))
             return default(T);
          
         StreamReader r = File.OpenText(fileLocation + "\\" + fileName + ".xml"); 
         string _info = r.ReadToEnd(); 
         r.Close(); 
         string _data = _info; 
         Debug.Log("XML File loaded : " + fileName); 
          
         return (T)DeserializeObject<T>(_data); 
     }

for reference, i save all of my files under the "Data" folder in the path location.

So, how can I modify this to get android to find, access, and mod the files?

Comment
Add comment · Show 7
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 hiddenspring81 · May 29, 2013 at 07:07 PM 0
Share

What type of data are you trying to read/write? If you want to safely read/write custom data at run-time, you're only option is to use Application.persistentDataPath. In my experience, trying to read/write to any other directory on Android has result in security exceptions at run-time.

avatar image prak · May 29, 2013 at 07:25 PM 0
Share

It's custom data classes (level name, score, powerup locations/types, level stats, ect). Ok, how do I make sure my data gets put in that directory pre-deployment? It doesn't seem to match application.datapath when working locally

avatar image hiddenspring81 · May 29, 2013 at 07:30 PM 1
Share

Are these files that are setup at build time, such as config files, or are these files that are being generated at run-time, such as save files? I think that by default, the Application.persistentDataPath is on the user's device is empty by default.

If you want to read a read-only file, I recommend placing it in a Resources directory, and using Resources.Load() to load it at run-time. If you want to read/write a file, you'll need to store it in Application.persistentDataPath.

avatar image prak · May 29, 2013 at 07:34 PM 0
Share

These are config files (they are not generated at runtime)... they get modified though. Seems like i may need to copy data over to the Application.persistentDataPath on first launch. I've never used Resources.Load() to access an X$$anonymous$$L file. Is that possible...?

avatar image hiddenspring81 · May 29, 2013 at 08:13 PM 1
Share

I'm 99% sure that data in Resources is read-only. If you want to modify it, read it from Resources, and then create a new, writable copy in Application.persistentDataPath.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hiddenspring81 · May 29, 2013 at 08:11 PM

What type of data are you trying to read/write? If you want to safely read/write custom data at run-time, you're only option is to use Application.persistentDataPath. In my experience, trying to read/write to any other directory on Android has result in security exceptions at run-time.

It should be noted that Application.persistentDataPath is empty on the user's device by default (and I don't think that there is a way to override this behavior).

Comment
Add comment · Show 1 · 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 mdunstetter · Nov 25, 2015 at 02:43 PM 0
Share

I am not sure if I understand. I have a csv file with some texts to handle translation en/fr so I put my csv in the Asset folder and access it with Application.dataPath (doesn't work on Android). How am I supposed to use Application.persistentDataPath ? I can save a file at Application.persistentDataPath, but my file already exists the first time my game starts, so I need to access it the first time if I want to save it somewhere else, so back to square one.

EDIT : Resources.Load() worked for me. After reading this whole page my conclusion is Resources.Load() is the alternative to Application.dataPath for mobiles, then Application.persistentDataPath is here if we need to back up files after the first play.

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

15 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

Related Questions

How to read json file in android game? 1 Answer

Path for Assets/Textures folder File in Android 2 Answers

build file with one game and two game can access it 0 Answers

Accessing custom resources on Android 1 Answer

Android StreamingAssets File Access 6 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