Is there any way to read or write to an external file? (i.e. save and load user preferences.) I’m looking to do this locally and not accessing a web server. If the WWW class supports file:// protocols as well, great, but the API spec doesn’t suggest this… and WWW doesn’t support writing to files.
You can use the PlayerPrefs class to tore and read configuration. (The preferences will be stored in the registry on Window ans inside a plist file in ~/Library/Preferences on Mac)
For arbitrary file IO, you can use the System.IO namespace in the .Net framework.
Please take a look at the following documents at MSDN to get an idea how to access local files:
How to: Read Text from a File
How to: Write Text to a File
File and Stream I/O
Basic File I/O
How to: Read and Write to a Newly Created Data File
Excellent, thanks for the help Keli!
Another tidbit you’ll probably want to use for custom file IO is Application.dataPath:
http://unity3d.com/Documentation/ScriptReference/Application.dataPath.html
On Mac OS X, at least, it’s not acceptable to write to your application’s bundle, but you can read data from here.
You’d want to store stuff in the user’s Application Support or Preferences folder, depending on what it is.
I have no idea where you’d stick stuff on Windows. Someone will have to chime in about that one.
cheers,
-Jon
You could just create a custom prefs or data folder in you app’s parent folder. There is no bundle-like setup on windows, so just make a folder.
A lot of games and apps install user preferences in the My Documents folder for the current user so different users can have different prefs. Don’t ask me where to find the username of the current user. Registry for sure, but what key?
-Jeremy
You can use .NET to find that:
Environment.GetFolderPath(Environment.SpecialFolder.Personal);
Or something.
Are you guaranteed write access to the app’s parent folder on Windows? On Mac OS X you definitely are not.
-Jon
AFAIK. Usually the only files/folders that are locked are the system files set to read-only, but you can write to them as well I belive.
Every mini-test-app I have written could write data to it’s parent folder if I remember correctly, but don’t hold me to that. Was a long time ago.
-Jeremy