At the moment, there is no platform-independent way to save data inUnity3D .
If you want to open a file for reading or writing, you need to use the proper path. This code will do the trick and works on PC, Mac, iOS and Android:
string fileName = "" ;
#if UNITY_IPHONE
string fileNameBase = Application. dataPath . Substring ( 0 , Application. dataPath . LastIndexOf ( '/' ) ) ;
fileName = fileNameBase. Substring ( 0 , fileNameBase. LastIndexOf ( '/' ) ) + "/Documents/" + FILE_NAME;
#elif UNITY_ANDROID
fileName = Application. persistentDataPath + "/" + FILE_NAME ;
#else
fileName = Application. dataPath + "/" + FILE_NAME;
#endif
fileWriter = File. CreateText ( fileName) ;
fileWriter. WriteLine ( "Hello world" ) ;
fileWriter. Close ( ) ;
[edit]
Since Unity 3.3, it is no longer necessary to use platform-specific code for simple file I/O. Use Application.persistentDataPath to do the trick.
This code would replace the code above:
string fileName = Application. persistentDataPath + "/" + FILE_NAME;
fileWriter = File. CreateText ( fileName) ;
fileWriter. WriteLine ( "Hello world" ) ;
fileWriter. Close ( ) ;
Thanks, erique, for pointing this out on the Unity forum .