I was looking through a few different solutions and I wanted to inform the community: there is a Unity-method now!!!
You will have to install the runtime scene serialization package. It is still in experimental so you’ll have to go to your project on disk and open ProjectSettings > manifest.json to add the following line:
"com.unity.runtime-scene-serialization": "1.0.0-exp.3",
To test it out, create a gameobject with 2 capsule colliders and add a script with the following script:
public class SerializationTest : MonoBehaviour
{
[ContextMenu("Try Json Serialization")]
public void TryJsonSerialization()
{
var capsuleColliders = GetComponents<CapsuleCollider>();
var json = SceneSerialization.ToJson(capsuleColliders[0]);
SceneSerialization.FromJsonOverride(json, ref capsuleColliders[1]);
}
}
And finally, you can push data from a file onto another.
The application for “RuntimePresets” here would be to store Json files with the settings you want. This has a few advantages over the proposed solution:
- supposedly works for ScriptableObjects (though untested).
- doesn’t use reflection, which is slow and can cause some serious issues to go unnoticed.
- json files take much less memory to store than prefabs.
- you can omit settings if you like.
- translates better to version control.
Demonstration:
Rewrite “SerializationTest” to the following:
public class SerializationTest : MonoBehaviour
{
public TextAsset lolStateXD;
[ContextMenu("Try Json Serialization")]
public void TryJsonSerialization()
{
var capsuleCollider = GetComponent<CapsuleCollider>();
SceneSerialization.FromJsonOverride(lolStateXD.text, ref capsuleCollider);
}
}
Then add a json file inside your assets folder with the following text:
{
"$type": "UnityEngine.CapsuleCollider, UnityEngine.PhysicsModule",
"center": {
"x": 0,
"y": 0,
"z": 2
},
"radius": 0.2,
"height": 0.4,
"direction": 2
}
Now reference it in your lolStateXD
and watch the magic happen!
Before
After
As you can see it edited the settings we wanted and only the settings we wanted. Enabled, IsTrigger, Material, all stay intact since we didn’t reference them.
I hope to work a little on making some QoL improvements e.g. saving settings in-editor through the context menu, but until I do I wanted to share this find with everyone on the forums. Not a lot of people seem to know about it and it’s really great that Unity is FINALLY getting a save solution like this.