Data Persistence with Unity UI?

Hello Unity Community,

I am trying to implement a save/load function into my game. Most of my code is heavily based off of Mike Geig’s Data Persistence tutorial (https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading).

I was able to use that logic to get some aspects of my save/load function working, but I am unable to serialize UnityUI type data. In particular, I am trying to save/load the positions and states (whether or not they are being displayed, expressions, etc.) of UI Images that are attached to a Canvas. Would anyone have any tips as for how to get started on this?

Your UI is most likely displaying data from your game, so all you have to do is recreate the gamestate. If not, you should design your UI to do that. If it’s a pure UI game, then your options would be a custom serialization. Unity has an Interface ISerializationCallbackReceiver in UnityEngine.Serialization namespace that you can implement to serialize whatever you need yourself.

Hi ValooFX,

Thanks for the reply. I do think that recreating the gamestate would be ideal for my game. Is there any tutorial/ documentation to assist with this? I’ve tried looking around, but have not had much luck finding anything to save/restore a gamestate.

Unity doesn’t come with a ready made SaveGame solution like unreal. But nonetheless there are a few similar ways to do it in Unity. For most simple games it’s enough to save state data to the PlayerPrefs and create your own methods to restore it from these values. Or, write it to an xml file (a little harder, but easier to debug). Or a custom file which you could serialize binarydata into and out, using the system class for stream reading/writing. But after all, you have to design it yourself because it highly depends on the way your classes are structured, there’s no generic way save and restore. A good advice i think is to make your system data driven, that is, create systems that operate on data; but I’m not the best person to speak to about this, as i’m also still learning how to do that properly. For a start, i would look at the way your game starts up, advances in levels, shuts down etc, and try to abstract the operations that you perform there into parts that read data and parts that do things with it (create variables for everything you want saved, that isn’t already represented as one). Then see if you can pack the different variables etc. that represent the gamestate into bundles you can associate with the systems they belongs to via save (read variables, save to file/PlayePrefs) and load (read data from file/PlayerPrefs, assign back and call initialization functions) methods and design your initialization methods in a way, that they can start ‘from zero’ given those bundles (instantiate objects, load/fill lists, etc). And design you UI so that it ‘looks at’ the data (don’t control UI elements from within your gamecode, use separate UI classes that use Properties and methods from your gamesystems).