When I call new SerializedObject(obj) where 'obj' is a UnityEngine.Object instance, as I understand it this is what it does:

Each serializable property of the object is traversed, and its sub-properties recursively. If the property is a reference to a UnityEngine.Object, it serializes the reference, not the object (simply its GUID or instanceID).

However, what if I want to serialize a single property like a UnityEngine.Object reference? There must be an internal method for serializing/deserializing values, but I don't know what it is or if there's possibly a better way.

This was brought about when I wanted to store a UnityEngine.Object reference as a string, so I could load it later on as an Object. This got me thinking that the only way I could achieve this was to contain the reference within another UnityEngine.Object and serialize that, but this struck me as quite an odd approach.

I've also encountered an unusual scenario where I had a very complex and dense serializable object, but I only wanted a single SerializedProperty. The only way it seemed was to serialize the entire object, causing noticeable lag which could have been prevented were there a more appropriate method.

So... to phrase my question succinctly, how do I serialize a single property (including UnityEngine.Object refs) without using SerializedObject or EditorJSONUtility?

  • \$\begingroup\$ One possible wrinkle here is that the serialized reference to a single object might not hold or keep any meaning without the rest of its containing context. As a simple example, imagine that when serializing a scene file, each object reference is treated like a number in the hierarchy traversal order. This lets the same scene be deserialized unambiguously, but it does not guarantee that a single reference serialized separately will still be valid if the scene is modified and re-serialized. \$\endgroup\$
    – DMGregory
    Jun 22, 2017 at 12:26
  • \$\begingroup\$ I'm pretty sure that the instanceid assigned to an object is unchanging within a scene, besides deleting and adding new ones. Otherwise I don't understand how Components could store references to other scene object instances. \$\endgroup\$
    – hedgehog90
    Jun 22, 2017 at 14:09


You must log in to answer this question.

Browse other questions tagged .