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
4
Question by darcerail · Nov 18, 2013 at 06:39 PM · androidloadingsaving

Trying to save and load from a text file on android.

This what I have written so far and it works fine in the editor but when I try it on my android phone it doesnt work. So what am I doing wrong?

using UnityEngine; using System.Collections; using System.IO; using System.Text;

public class AndroidScoreSavingTest : MonoBehaviour {

 string message;
 string loadMessage = "Yeah working";
 string data;
 FileInfo f; 
 
 void Start()
 {
     f = new FileInfo(Application.dataPath + "\\" + "myFile.txt");
     Screen.SetResolution(800, 600, true);
 }
 
 void OnGUI()
 {
     GUILayout.BeginArea(new Rect(0,0,500,500));
     GUILayout.Label(message + " " + data);
     if(GUILayout.Button("Save"))
     {
         
         if(!f.Exists)
         {
             message = "Creating File";
             Save();
         }
         else
         {
             message = "Saving";
             Save();
         }
     }
     if(GUILayout.Button("Load"))
     {
         if(f.Exists)
         {
             Load();
         }
         else
         {
             message = "No File found";    
         }
     }
     GUILayout.EndArea();
 }
 
 void Save()
 {
     StreamWriter w;
     if(!f.Exists)
     {
         w = f.CreateText();    
     }
     else
     {
         f.Delete();
         w = f.CreateText();
     }
     w.WriteLine(loadMessage);
     w.Close();
 }
 
 void Load()
 {
     StreamReader r = File.OpenText(Application.dataPath + "\\" + "myFile.txt");
     string info = r.ReadToEnd();
     r.Close();
     data = info;
 }

}

Comment
Add comment · Show 1
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 bruno-ribeiro · May 08, 2017 at 04:11 AM 0
Share

Your Load() method was exactly what I was looking for. Thanks for that!

3 Replies

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

Answer by GameVortex · Nov 18, 2013 at 07:01 PM

On android you might not have direct write access to the dataPath of the project.

Try using Application.persistentDataPath instead. This is where save files are intended to be stored.

Comment
Add comment · Show 3 · 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 darcerail · Nov 18, 2013 at 07:51 PM 0
Share

That worked thank you.

avatar image GameVortex · Nov 18, 2013 at 08:01 PM 0
Share

That is great! Could you mark it as answered so it gets removed from the unanswered section? =)

avatar image newleon · Mar 08, 2016 at 09:31 AM 0
Share

Great solution. It perfectly worked for me. Thanks.

avatar image
1

Answer by TheMaster001 · Mar 11, 2018 at 01:38 PM

yep that worked need to change the Application.dataPath to Application.persistentDataPath like you said and it worked a treat , many thanks

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
avatar image
1

Answer by AquinoJ · Apr 10, 2019 at 08:41 PM

I needed to load json and similar text files into my Android game. Unfortunately I could not access the internal memory from the Application.persistentDataPath, so when I searched, I found this amazing tool that helped me use these functions, and it only cost $ 9.90. Now I can read and write files in internal memory, manage files and folders, and so on.

https://assetstore.unity.com/packages/tools/integration/native-android-toolkit-mt-139365

I hope this helps anyone who is reading in the future!

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

22 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

Related Questions

Saving and loading to/from Android 1 Answer

Saving frequently throughout game lifecycle (Android) 1 Answer

Saving And Loading on Android 0 Answers

JSON file not written/read on application exit on android 1 Answer

Serializing a graph 2 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