简体   繁体   中英

Unity I/O with Android device

i can read and write file with this method for storing game progres.

filePath = Application.dataPath + "/";
filenam= "SavedGame";
extension = ".txt";

Lettura ----------------
var FRead = new File.OpenText(filePath + fileNam + extension);

for (var i : int= 0; i<N; i++) {

FRead.ReadLine();

}
Scrittura--------------------
var FWrite: StreamWriter = new StreamWriter(filePath + filenam + extension);
FWrite.WriteLine("AAAA");

A run time in the Unity editor all work perfectly.

But in Android device Application.dataPath seems dont work.

I tryed Application.persistentDataPath that work fine but in (Read Only),but i need place the SavedGame.txt manually in the patch.

I tried a fantomatic "jar:file://" + Application.dataPath + "!/assets/"; but dont work.

Note when i build the project in apk packege semms dont appear my SavedGame.txt,that in the editor is in UnityProject\\Assets .

Come posso risalire al percorso giusto del mio SavedGame.txt, posizionato in ad esempio in UnityProject\\Assets nei device?

How i cand do this in a correct way read and write SavedGame.txt in android device?

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.

Change:

filePath = Application.dataPath + "/";

to

filePath = Application.persistentDataPath + "\\";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM