Hi, I’ve made a level editor for a game I just need to know how to save levels. I’m not extremely good at scripting so if you help me can you tell me the basics of the script?
Well the format you use is arbitrary. So you may want a text file with each line corresponding to some object placed in the world or can be more complicated with XML. In either case you’ll want to use the .NET System.IO group, which handles reading and writing of files. See http://www.go-mono.com/docs/ Open up the Class Library and find System.IO. Any of the Reader and Writer classes will work for you, though the BinaryReader/BinaryWriter won’t produce human readable files.
So an example: Use the File class to get a FileStream handle to a file then use that stream as the target for the StreamWriter. To read the file back in you’ll do the same thing, use the file class to get a FileStream pointing at the file you want to read, then hand that to the StreamReader. Then use the StreamReader methods to read in each line from the file.
Text files can be used with the TextAsset class now in 2.0, so you don’t need .NET to read them. Although I think you still do for writing.
–Eric
I don’t understand. How can this be used in Unity?
Because Unity uses Mono (the open-source implementation of .NET).
–Eric
Could I have a script example? Could You tell me how to use it in Unity?
–Eric
Wow! How Could I make it load a level from a list?
That’s kind of a general question. You’d make a file requester using the new GUI tools in 2.0, and then parse the selected text file, basically.
–Eric
Ok. Another thing: How can I make it save the level I just created in the level editor?
Again, I can’t answer that specifically since it depends on your game. I expect you’d decide on some kind of format for level information, and you’d make a file requester where the user enters a name, then you go through the scene and save the information to a file according to the format you’ve decided on.
–Eric
Sorry for being such a noob but could you give me an example of how to save the level infomation?
Alas no, that’s a little too involved for a quick example, sorry. But this is a relatively straightforward task, so once you pick up some scripting, it shouldn’t be too hard. One step at a time.
–Eric