How to Serialize Content Placed at Runtime for Persistence
This how-to demonstrates:
- Registering prefabs for spawning at runtime
- Storing transform and stateful data for restoring content in a future session
- Updating previously serialized data
This how-to uses manually constructed verbose strings to demonstrate serialization in a human-readable format. For proper serialization, we recommend using [Serializable]
structs with a JSON serializer for a concrete structure or a binary serializer for compact storage.
Prerequisites
You will need a Unity project with Lightship AR enabled. For more information, see Installing ARDK 3.0.
Basic Concepts for Serialization of Runtime Content
As opposed to Remote Content Authoring, where Unity scenes contain static placed objects in each ARLocation
, runtime content serialization allows players to place content arbitrarily, then serialize the placed content into a known format so that it can be re-spawned in the future.
To serialize a player-constructed scene, this information is needed:
- The root transform that anchors the content (ARLocation, Image Target, etc);
- This ensures that future deserialized content will be placed in the same location.
- All
GameObjects
that the player has placed; - The type and state (transform and color, in this example) of each of these
GameObjects
.
This data is then compacted into a string or binary blob for retrieval the next time a player tracks that same ARLocation
.
To deserialize the data and reconstruct the scene:
- Retrieve and instantiate each
GameObject
from the serialized data;- Each
GameObject
is placed under the same root transform (ARLocation
) so that it appears in the same place.
- Each
- Apply the stateful data (transform, color, etc.) of each
GameObject
.
A sample scene, "VpsSceneSerialization", is provided to follow along with the scripts in this How-To.
Registering Prefabs for Serialization
The first problem to solve is gathering a list of all possible Prefabs that players can place. This is necessary to reconstruct the scene in the future.
The script PersistentObject.cs
allows for finding spawnable Prefabs with a GetComponent
and identifying each unique Prefab with a PrefabId
.
Click to expand the PersistentObject class
using UnityEngine;
public class PersistentObject : MonoBehaviour
{
[SerializeField]
private long _prefabId;
public long PrefabId
{
get { return _prefabId; }
internal set
{
_prefabId = value;
}
}
... // Serialization code
}
In the "VpsSceneSerialization" sample, there are two PersistentObject
Prefabs, a cube and a sphere,
assigned the PrefabIds
0 and 1 respectively. These PersistentObjects
are then registered to a PersistentObjectManager
to create a complete list of spawnable objects.
Click to expand the PersistentObjectManager class
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class PersistentObjectManager : MonoBehaviour
{
[SerializeField]
private List<PersistentObject> _persistentObjects = new List<PersistentObject>();
public ReadOnlyCollection<PersistentObject> PersistentObjects => _persistentObjects.AsReadOnly();
... // Serialization and validation code
}