Read and Write textfiles at runtime

I’m making a puzzle game (which is somewhat strange to explain and not needed right now) but I want that the levels of the game are read from text files, but also can be created and saved to textfiles, at runtime (with a simple ingame editor).

My initial approach (as I was coding the read part) was to put the file in the resources folder, and read them at runtime with Resources.Load. This works as needed. However, the problem is if I want to save new files and read them. From what I’ve seen the Resouces are read only when at run-time, which makes this more complicated. I know I can use the mono/C# IO, but I would only use that as last resort, because I would like it to work with the web player.

When on the webplayer there is no need to actually mantain files between sessions, but when on Windows it would be usefull.

Here is my current read code: (the t lines are read in another function)

	public Map(string filename){
		TextAsset puzdata = (TextAsset) Resources.Load("Levels/" +filename, typeof(TextAsset));
		if(puzdata == null){
			Debug.Log("no file found!");
			return;
		}
		StringReader reader = new StringReader(puzdata.text);
		//testing
		if ( reader == null ){
			Debug.Log("puzzles.txt not found or not readable");
		}

		else{
			// Read each line from the file
			name = filename;
			string txt;
			while ((txt = reader.ReadLine()) != null){
				string[] line = txt.Split(' ');
				if(line[0] == "s"){
					Vector3 size_temp = new Vector3(float.Parse(line[1]), float.Parse(line[2]), float.Parse(line[3]));
					size = size_temp;
					mapContent = new MapChunk[(int) size_temp.x, (int) size_temp.y, (int) size_temp.z];
				}

				else if(line[0] == "p"){
					startingPos = new Vector3(float.Parse(line[1]), float.Parse(line[2]), float.Parse(line[3]));

					startingPosChunk = MapChunk.ChunkPosition.Down;
					if(line[4] == "d")
						startingPosChunk = MapChunk.ChunkPosition.Down;
					else if(line[4] == "l")
						startingPosChunk = MapChunk.ChunkPosition.Left;
					else if(line[4] == "b")
						startingPosChunk = MapChunk.ChunkPosition.Back;
				}
			}
		}
	}

And here is a example of a level file:

s 10 10 10
p 2 2 2 d
t 0 2 1 d N
t 1 2 2 d N
t 2 2 2 d N
t 3 2 2 d D
t 4 2 2 d N
t 3 2 3 d N
t 2 2 0 d N
t 3 2 1 d D
t 1 2 1 d D
t 4 2 3 d E
t 2 2 1 d E
t 0 1 0 b N
t 1 1 0 b N
t 0 0 0 b N
t 1 0 0 b N
t 2 0 0 b N
t 3 0 0 b N
t 3 1 0 b D
t 4 0 1 l N
t 4 1 1 l N
t 4 1 2 l N
t 4 0 2 l N
t 4 0 3 l N
t 4 1 3 l N
t 1 2 2 l N
t 0 2 1 l N
t 0 2 1 b N
t 1 2 2 b N
t 2 2 2 b N
t 0 2 0 l N

I would like to create them in a way that both the files created ingame and the ones created outside of the game and put in the assets would be read by the same function/code.

Is there any way I can do this?

Sorry for the english and thanks for the help.

You can save string with PlayerPrefs (Unity - Scripting API: PlayerPrefs)

Because of security reasons, you won’t be able to save, create or read files from the host computer from a WebPlayer application.

Yes but I really want to use files. Forgetting Web Player then, is there any way I can write files do resources at runtime?

Yup - System.IO. http://msdn.microsoft.com/en-us/library/system.io(v=vs.90).aspx

Ok, already found a soution, simply wont let people do some stuff with #if UNITY_STANDALONE_WIN, use Resources.Load for the standard levels, and System.IO for the editor.

Thanks for the help.

What is the path I can use to store a file in a relative path to the game? Also, is there any way I can manualy put files in this path before compiling?

http://docs.unity3d.com/Documentation/ScriptReference/Application-dataPath.html

http://docs.unity3d.com/Documentation/ScriptReference/Application-persistentDataPath.html