In order to create a game that anyone would want to play I need to implement a feature allowing the user to save their restaurant upon leaving the game and then loading it when the game is relaunched.
I decided quickly I wanted to implement this feature by saving all relevant data into a text file and then read the text file into the game to load in the relevant game objects.
I wanted to create my save feature like this so it would have little impact on memory and would be very quick to save and load up games.
Saving a Game
Saving my game is simply the result of writing data to a text file using the stream writer asset included within Unity's System.IO framework. The stream writer class requires a path, location where the file will be saved, and a file name. I only allow one save file per user therefore the name of the file is simply savefile.txt.
With a stream writer open to the desired location I now start adding string data to the text file. The data I need for my game assets are it's position, rotation, texture index mapping to my texture database (used to save wallpaper/tile textures), the item's spawned status (determines if walls were pre-placed or user placed) and the assets name.
This data would be used to locate the prefab via the asset name, instantiate the game object using the position data, rotate the object with the rotation data, locate the objects texture from my database using the texture index so it looks exactly the same and set whether the object can be sold using the spawned value.
Once I have written all the lines of text I want to the text file I need to close the open channel to the text file by simply calling the streamwriter.close method.
Figure 1
You'll notice in figure 1 that my data is broken up by apostrophes. The reason I have formatted my text like this is so that when I read the data back into my game I have an obvious character I can use to break the line back into multiple strings. I'm able to read in all the characters until the apostrophe is reached and then add the next characters to a new string value.
Loading a Game
Loading in my game is virtually identical to saving my game expect in reverse. This time I will be using the stream reader asset from Unity's System.IO framework. The stream reader reads in all the data from the text file line by line and I store it to a string. In order to separate the data I create a new array of strings to separate the file into lines using '/n' as the separator. This means that whenever a new line, represented by /n, is detonated I split the the string and add it to my array.
Once I have gone through the text file and separated out all the lines of text I need to separate the values. I used an if statement to cycle through the array of lines and then separated the line into a new array using an apostrophe as the separator.
I know the first string separated will always contain the asset type therefore I can create a series of if statements to identify the type of asset the line has data for by comparing the string to asset names. For example If 'assetname == "Wall"' returns true I know the line contains data for a wall and can handle the data accordingly.
Lastly, all I need to do within these if statements is instantiate a new prefab populated with the data read from the file. Assuming all is as expected when a user saves their game all data will be written to a text file and read back in at a later date. The game should appear to be exactly the same therefore fulfilling the effect the game state has been saved.
















