Path check if (!Directory.Exists("Saves"))
#New version of unity web player free download code#
OK, this is a lot of code at once, let’s break it down. LocalCopyOfData = (PlayerStatistics)formatter.Deserialize(saveFile)
LocalCopyOfData = įormatter.Serialize(saveFile, LocalCopyOfData) įileStream saveFile = File.Open("Saves/save.binary", FileMode.Open) We need to go into our GlobalObject (or similar object you should have): //In global object:īinaryFormatter formatter = new BinaryFormatter() įileStream saveFile = File.Create("Saves/save.binary") Now, let’s write the functions that will serialize and deserialize data. We have also added our Scene ID and Position values. This tells the Engine that the data in this class is suitable for writing down in binary form, or “serializing”. Let’s get the obvious out of the way first: We have a custom “attribute” declaration in front of the class.
Public float PositionX, PositionY, PositionZ Scene ID and Positionįirst, we need to solve the two biggest problems as listed above: Scene ID, and position of the player within the scene.Īssuming we have a class that holds our player’s data, like this: public class PlayerStatistics Let’s tackle the new functionality step by step. This tells us if we need to copy the loaded data, or leave it alone. Player Control class at Starting (Scene Loading event) always checks if maybe the scene is Loaded from a save game, or anew.It also carries the copy of a saved player’s data. Global Control now always carries a publicly available bool that states whether we are loading a scene, or starting it anew.It’s a class that’s responsible for the player’s input: This explains the flow of the program from a PlayerControl class, which is the primary class we’ll be dealing with today. This may sound a bit disorienting so let’s break this down into a flowchart. For this, we need our GlobalObject class which persists between scenes because we will need to load the data and set the variable, before initializing the loading procedure of the scene the player was on when they saved the game. Saving/Loading procedure Keeping it simple for now, we will assign two hotkeys for save and load: F5 and F9 respectively.įresh start or load? We will need to keep a boolean value which tells us if the scene has been loaded, or started anew. We need to add three new float values denoting the player’s X, Y, and Z position instead, and apply them to the position vector of the player when we are loading data. Scene position? Unfortunately, we cannot add a Transform or Vector3 object into the Player’s data package class because they are not serializable objects. Scene identification? Adding a new Integer variable into our Player’s data package class, so we know what scene the player was on.
How do we know if we need to start the level anew, or load existing data?.How do we initialize the Loading procedure?.How do we initialize the Saving procedure?.What scene was the player on when the game was saved?.We need to consider the following problems: Using the project from our previous example, we will need to prepare some things in order to begin writing this functionality correctly. Location in the scene where the player was when the game was saved.