MemoryStream memorySteam = new MemoryStream() Create memory stream that the Binary Formater relies on Serialization is converting a class or data structure into bytes or a data format which can be saved. Taking more time to save to the hard drive and load. This array can be super big, imagine a file, it can reach gigabytes. It’s going to allow us to convert a class’s variable contents to an array of bytes. But what bytes should I be saving and loading and how do I read it? This is where binary serialization comes in. What now? Well we can now save and load bytes. Then using File.WriteAllBytes and File.ReadAllBytes to read and write. For loading if it doesn’t then it just returns null, nothing. Next we just do self-explanatory things such as checking if the path exists for saving, if it doesn’t then create the path and file. Then we create another folder called OurGame_Data, this should be replaced to anything you want or nothing at all and save in that folder itself. #UNITY 2019 PLAYER PREFS CODE#}This code is creating a file at the Application.dataPath. String path = Application.dataPath + "/" + "OurGame_Data" String filePath = path + "/" + file + ".dat" įileStream newFile = File.Create(filePath) This is using System.IO, so its quite simple. Saving and Loading Next lets write the code to actually save and load bytes. From Lists, Dictionaries, to ints, to structs to other classes. If you can see it in Unity’s inspector it can handle it, along with a few extra. Binary Serialization lets you serialize quite a number of data types. }This example class has two variables, and integer and float. The DataModel is simply a class in this example. PlayerProgress containing the scene they left in, position, level, weapon, etc. GameOptions containing resolution selection, volume, quality options, etc. It’s smart to have multiple data model types, for example one for GameOptions and PlayerProgress. It is going to contain any information that needs to be saved. The data model is what we will be saving in our data file. First we want to create our Data Model(s). JSON and XML are common ways, for this example however we will be just saving it as bytes. There are different ways you can serialize. Serialization is a way of converting an object into a stream of bytes, in order to save or read. I thought I’d mention this post to show how you might be able to expand on what we’re covering here. Getting Started In my last Blog post, “ Dynamic Objects Saving and Loading“, I go through the process of doing everything but the core file saving and loading part. #UNITY 2019 PLAYER PREFS HOW TO#I decided I’d do a blog post walkthrough of how to go about binary serialization and saving in Unity. A topic I was really excited to cover was a saving and loading system that allows for more robust data saving. We weren’t able to complete the workshop however we did cover a lot of ground considering the audience and what we did cover was covered extensively. Introduction This weekend I hosted the Game Dev Knights of University of Central Florida Unity introduction workshop.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |