UnitySerializer is a full level serialisation plug in. It enables you to save the progress of a game and restore it at some point in the future. It does this with minimal impact on the design of your game and requires minimal modification to your behaviours and classes. The package also contains a “room” manager that helps you save the states of scenes that the player has left and subsequently re-enters.
Works on all Unity runtime platforms including IOS and Droid. If you are running on an AOT platform (like iPhone or Android) you should check out this post to do with player settings that may be important based on your coding style.
The serializer supports storing newly instantiated prefabs, as well as the status of existing scene objects (such as those that might be destroyed during the course of a game).
UnitySerializer supports the serialization of your custom classes, fields and properties including things like generic collections of complex objects as well as standard Unity components including
- Animation support
- Rigidbody support
- Lights, Joints, CharacterController
- Materials & Textures (references and programmatically generated)
- Support for NavMeshAgent
- Transform – including parent/child relationships
- JSON or compressed binary formats
The status of your level is stored in a string which can be placed in PlayerPrefs – stored on a server or in a file.
For those interested in the details of extending the serialization methods, some advanced features or for those who would consider contributing to the project there is a technical discussion of the serialisation implementation available here.
YOU ARE LICENSED TO USE THIS CODE FOR ANY PURPOSE – THERE ARE NO WARRANTIES EXPRESSED OR IMPLIED. MIT LICENSE. USE AS YOU SEE FIT. If you use this in a commercial project or are a professional studio I would appreciate a donation – it is not required – but bug reports from supporters will be prioritised. If you use this code as part of another framework that you distribute please add a link to this page and a credit to me as the author of that section.
Please email me if you wish to contribute time to this project.
Save game for Angry Bots in less than 2 minutes!
In this video I just save everything, when you save your own level – pick and choose the things you want to save to keep performance higher and saved games smaller.
Version 2.0 features
If you want to learn about the new features in version 2.0 including JSON support you should read this page.
Unity Serializer Menu & Wizard
There is a Wizard that can help you configure your game and a new Unity Serializer menu bar will appear after you have imported the package. You can use the wizard to help you setup your scenes. The Wizard also contains maintenance commands to delete saved games.
There are also short cut keys to add a new script to the current object. I strongly suggest you use the wizard for most scene configuration.
You will also be able to see colour coded information in the hierarchy view showing what script is attached to each of your game objects.
Unity Serializer References Wizard
The new page of the Unity Serializer wizard helps you to identify areas that might make saving and loading your game less robust. Unity Serializer stores Asset references by using their type, their name and an index (so if there are three things called Take 001 – each one has an index starting from 0). It makes your save games more robust if you keep each of the items you will refer to uniquely named. It is not a requirement that they are unique it just improves the robustness of your saved games if they are. The page on the wizard highlights items with matching names and lets you click on them to view them in the project window. Items shown in red are internal to Unity and are hence quite possibly going to change more often.
NOTE: You will probably see a lot of things in this window that you never use in a reference. You can just ignore those.
iTween support is downloadable as an add on module. It enables the storage and restart of any tweens running on objects that have at least StoreInformation attached to them
In order for this to work it is necessary to add 2 lines of code to iTween. Find the IEnumerator Start() function in iTween.cs and insert the code:
if(LevelSerializer.IsDeserializing) yield break;
You will need to do this each time up upgrade iTween.
I’m a big fan of PlayMaker and given that many PlayMaker users that aren’t programmers may have a problem adding Load/Save functionality I’ve developed a plug in that you can download to enable PlayMaker Finite State Machines to be saved. Once the plug in is installed you just have to attach a Store Information script to the objects that have the FSMs attached.
The plugin saves all of the local and global variables in addition to the current state of the FSM and restores all of these on load. Note that even if you only save 1 FSM, all of the global variables will of course be loaded – this might lead to strange behaviour so it is probably better to make sure you store all of your PlayMaker enabled objects.
NEW PlayMaker Actions & Demo Scene
The PlayMaker add on now has custom actions for PlayMaker to save games and checkpoints. The package also includes an example scene that uses these actions to make a rudimentary save game menu.
I’m adding support for as many types as possible – but I may not cover every case. Don’t worry – you can still serialize your levels, but you might have to do some work
Say you create a mesh and change the mesh of a renderer to the runtime constructed one – as of writing (version 0.84) you can’t serialize these meshes directly (it’s coming I hope!) However you could just store enough information to recreate the mesh in some variables on a script and then write an OnDeserialized() function which takes those values are recreates whatever you need.
I will strive to add as many types as possible – but OnDeserialized() is your friend because in there you can build anything you need that hasn’t been automatically saved.
Tolerant to change
UnitySerializer does it’s best to cope with you changing the public fields and properties of your class. Internally it tokenises the names of things and uses these to set the values back later so it can copy with quite extreme changes to structure – e.g. adding and removing large numbers of properties or fields. It’s not so good when you change the type of a field or property to one that is not compatible (castable) from the original stored value. If you need to change things like this I suggest you create a new property or field rather than reuse an existing name.
You can also store your files using Verbose mode – that will provide extra serialization information and does attempt to fix changing types.
To turn on Verbose mode you need to import Serialization and use UnitySerializer.Verbose = true;
In this mode you can also handle an event MapMissingType that allows you to supply a replacement type if it is semantically similar to a now defunct former type. UnitySerializer.MapMissingType += YourMissingTypeHandler;