Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Read and Write textfiles at runtime

Discussion in 'Scripting' started by manelis, Jan 7, 2014.

  1. manelis

    manelis

    Joined:
    Aug 15, 2013
    Posts:
    8
    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)

    Code (csharp):
    1.     public Map(string filename){
    2.         TextAsset puzdata = (TextAsset) Resources.Load("Levels/" +filename, typeof(TextAsset));
    3.         if(puzdata == null){
    4.             Debug.Log("no file found!");
    5.             return;
    6.         }
    7.         StringReader reader = new StringReader(puzdata.text);
    8.         //testing
    9.         if ( reader == null ){
    10.             Debug.Log("puzzles.txt not found or not readable");
    11.         }
    12.  
    13.         else{
    14.             // Read each line from the file
    15.             name = filename;
    16.             string txt;
    17.             while ((txt = reader.ReadLine()) != null){
    18.                 string[] line = txt.Split(' ');
    19.                 if(line[0] == "s"){
    20.                     Vector3 size_temp = new Vector3(float.Parse(line[1]), float.Parse(line[2]), float.Parse(line[3]));
    21.                     size = size_temp;
    22.                     mapContent = new MapChunk[(int) size_temp.x, (int) size_temp.y, (int) size_temp.z];
    23.                 }
    24.  
    25.                 else if(line[0] == "p"){
    26.                     startingPos = new Vector3(float.Parse(line[1]), float.Parse(line[2]), float.Parse(line[3]));
    27.  
    28.                     startingPosChunk = MapChunk.ChunkPosition.Down;
    29.                     if(line[4] == "d")
    30.                         startingPosChunk = MapChunk.ChunkPosition.Down;
    31.                     else if(line[4] == "l")
    32.                         startingPosChunk = MapChunk.ChunkPosition.Left;
    33.                     else if(line[4] == "b")
    34.                         startingPosChunk = MapChunk.ChunkPosition.Back;
    35.                 }
    36.             }
    37.         }
    38.     }
    And here is a example of a level file:

    Code (csharp):
    1. s 10 10 10
    2. p 2 2 2 d
    3. t 0 2 1 d N
    4. t 1 2 2 d N
    5. t 2 2 2 d N
    6. t 3 2 2 d D
    7. t 4 2 2 d N
    8. t 3 2 3 d N
    9. t 2 2 0 d N
    10. t 3 2 1 d D
    11. t 1 2 1 d D
    12. t 4 2 3 d E
    13. t 2 2 1 d E
    14. t 0 1 0 b N
    15. t 1 1 0 b N
    16. t 0 0 0 b N
    17. t 1 0 0 b N
    18. t 2 0 0 b N
    19. t 3 0 0 b N
    20. t 3 1 0 b D
    21. t 4 0 1 l N
    22. t 4 1 1 l N
    23. t 4 1 2 l N
    24. t 4 0 2 l N
    25. t 4 0 3 l N
    26. t 4 1 3 l N
    27. t 1 2 2 l N
    28. t 0 2 1 l N
    29. t 0 2 1 b N
    30. t 1 2 2 b N
    31. t 2 2 2 b N
    32. 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.
     
    Last edited: Jan 7, 2014
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
  3. manelis

    manelis

    Joined:
    Aug 15, 2013
    Posts:
    8
    Yes but I really want to use files. Forgetting Web Player then, is there any way I can write files do resources at runtime?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  5. manelis

    manelis

    Joined:
    Aug 15, 2013
    Posts:
    8
    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.
     
  6. manelis

    manelis

    Joined:
    Aug 15, 2013
    Posts:
    8
    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?
     
    Last edited: Jan 7, 2014
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539