- Home /
Android File-io?
Hello, I need help saving a text file to my android game
I'm using Application.persistentDataPath + "save.txt" but when I save it on my android phone I don't see the text file. It does have a folder on the phone for the game though. What am I doing wrong? Here is my function
var directory = Application.persistentDataPath + "/save";
function Start () {
if (!System.IO.Directory.Exists(directory)){
System.IO.Directory.CreateDirectory(directory);
save2();
}
}
var saved2 = Application.persistentDataPath + "/save/astroids.txt";
function save2(){
pla2 = GameObject.FindGameObjectsWithTag("planet");
var sw1 = new StreamWriter(saved2);
for( var pln1 : GameObject in pla2){
var pls1 : planetbehave = pln1.GetComponent(planetbehave);
sw1.WriteLine("---");
sw1.WriteLine("" + pln1.transform.position.x + "\n");
sw1.WriteLine("" + pln1.transform.position.z + "\n");
sw1.WriteLine("" + pls1.health + "\n");
sw1.WriteLine("" + pls1.sizes + "\n");
}
sw1.Flush();
sw1.Close();
}
First off, are you sure you are looking in the right folder? Try to write the Application.persistentDataPath to screen or something to check where the folder is.
This will also be helpful : http://answers.unity3d.com/questions/317048/android-writing-to-applicationpersistentdatapath.html
I'm sure I'm looking in the right folder, because it creates the folder. I have a root explorer on my phone and I still can't find the text file. It has all permissions set, and it should be writing the file, but it does not.
Have you checked both /data/data/com.mycompany.myapp/files and /mnt/android/data/*? What are your write access settings?
For the record, you are only calling save2() if the directory doesn't exists. The directory exists doesn't it? so it won't create that file until you remove the directory.
Answer by Bunny83 · Dec 16, 2012 at 01:22 AM
I used a lot of IO stuff on my android and even on the iPad. Never had any problems with it. However i don't have a rooted tablet and Unity stores the files in a directory which you can't access with a usual filebrowser. Are you sure that the file doesn't exist? You're actually not supposed to manipulate the files in the persistentDataPath outside of your app. Check from your app if the file exists, For my everything works as it should and even on the iPad it works the same way.
I'm going to add some debugging messages for gui text, and run it again. I'll let you guys know what if the game detects the file or not.
Yep, just adding this
function Update(){ if (System.IO.File.Exists(save1)){ exists = 1; }else{exists = 0;} } function OnGUI(){ if(exists == 1){ GUI.Label(Rect(Screen.width / 2, Screen.height / 2, 125,126), "Exists!");
}else{ GUI.Label(Rect(Screen.width / 2, Screen.height / 2, 125,126), "sorry bub!");
} }
And I'm getting the "sorry bub" message
In the log cat it's saying
"I/Unity (25983) IsolatedStorageException: Could not find a part of the path "/Saves/astroids.txt"
Never$$anonymous$$d. It was the old string getting stuck in the unity editor. >.< Gotta love unity. I fixed the problem and it's saving now! Thank you!
it's amazing how many Android folks assume you can "see" the contents of Application.persistentDataPath :)
Those dudes have a "rooted-is-normal" culture :)