We’ve all encountered this when trying to make a game before. You’ve sat at your desk thinking, “What is the fastest way I can implement a save system?”. Save systems are one of those things you need, but you don’t want to spend a bunch of time on — time that could be spent on developing core gameplay. In this tutorial we are going to learn how to make a simple save system with XML in Unity.
First things first, fire up your favorite editor and get ready to follow along (aka copy and paste).
For this project we need two extra dependencies, XML and Linq:
Next we create the function that deals with the XML.
It may seem like a lot of code, but trust me — its pretty simple. In the first try-catch block, we load the save file from the file_path. If it doesn’t exist, it creates a new one.
In the second try-catch, it tries to update an element with name as its name and insert value into it. If that fails, it makes a new element.
Finally, we add a SaveString function that makes XMLUpdate easier to call. We also define where to save the save file.
Thats it! In just above 30 lines of code we have created a robust, easy to use save system. Below is the full script that you can copy into a CS file.