Search Unity

In game editor and saving.

Discussion in 'Scripting' started by mehware, Dec 6, 2007.

  1. mehware

    mehware

    Joined:
    Nov 19, 2007
    Posts:
    739
    Hi,

    I have an in game editor for a 2D game thats tile/cell based. Each tile is a prefab. Is there a way to write out all the objects in the level as a "scene" file so I can use Application.loadLevel() when I want to load the level. Simpler, whats the level/scene file format?
     
  2. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Not to my knowledge. You might want to invest some time into creating your own custom save format. That could be valuable as you could use it to allow your players to create levels as well (if that is what you want).
     
  3. mehware

    mehware

    Joined:
    Nov 19, 2007
    Posts:
    739
    yeah I think all the level format needs for now is the x,y position of the cell and what prefab it is. Im looking into TextAsset for this.
     
  4. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    TestAsset is for read-only operations. You'll want to use Mono's I/O.
     
  5. mehware

    mehware

    Joined:
    Nov 19, 2007
    Posts:
    739
    This is the file format of my level/scene, the name of the prefab/object followed by the x y z position. An example is this:

    Code (csharp):
    1.  
    2. wooden_crate_prefab(Clone)
    3. -5.5
    4. 13.5
    5. 0
    6.  
    This is my code to read it in based from the thread from Erich

    Code (csharp):
    1.  
    2. if (GUI.Button (Rect (20,300,180,30), "Load Level")) {
    3.        
    4.         try {
    5.             // Create an instance of StreamReader to read from a file.
    6.             sr = new StreamReader("Assets/Level1.txt");
    7.             // Read and display lines from the file until the end of the file is reached.
    8.             line = sr.ReadLine();
    9.             while (line != null) {
    10.                 print(line);
    11.                 line = sr.ReadLine();
    12.                 if (line == "wooden_crate_prefab(Clone)")
    13.                 {
    14.                    
    15.                     line = sr.ReadLine();
    16.                     readInPos.x = float.Parse(line);
    17.                     line = sr.ReadLine();
    18.                     readInPos.y = float.Parse(line);
    19.                     line = sr.ReadLine();
    20.                     readInPos.z = float.Parse(line);
    21.                     Instantiate(woodenCratePrefab, readInPos, Quaternion.identity);
    22.                 }
    23.              }
    24.             sr.Close();
    25.         }
    26.         catch (e) {
    27.             // Let the user know what went wrong.
    28.             print("The file could not be read:");
    29.             print(e.Message);
    30.         }    
    31.            
    32.     }
    33.  
    Where woodenCratePrefab is a public GameObject and a prefab wooden crate is assigned to it through unity's editor and readInPos is a private Vector3.

    Its not Instantiating the object for some reason, any ideas?[/code]
     
  6. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    I suggest you use serialization. I got some help with an example in this thread (thanks Darko!):
    http://forum.unity3d.com/viewtopic.php?t=6929

    I have since put it to use and have my own structure of save-specific classes which can be used for replays as well. Works great!
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not sure if it helps, but I posted this, which reads in a text file using TextAsset and constructs a level from the contents. You'd have to use .net I/O for saving and loading external files of course.

    --Eric