![]() By default, Unity generates this ID automatically to specify an ID, use ManagedReferenceUtility.SetManagedReferenceIdForObject. Each managed reference has a unique ID, which the MonoBehaviour, ScriptableObject or other host object that contains the fields associated with it. The objects assigned to fields with the attribute in the host object are managed references. You can use SerializeReference directly on fields of the host object, or indirectly on fields of custom structures or classes that are serialized within the host object. In the context of SerializeReference, your object which specializes MonoBehaviour, ScriptableObject, ScriptedImporter or other UnityEngine class is called the host object. Using SerializeReference allows you to store null references.īy-value serialization is more efficient than using SerializeReference in terms of storage, memory, and loading and saving time, so you should only use SerializeReference in situations which require it. Without the use of SerializeReference, null values are replaced with an inline object that has unassigned fields in the serialized data. Value-based serialization can't represent null. You can see polymorphism in the SerializeReferencePolymorphismExample class below. If you try to use polymorphism on custom serializable classes without the attribute, your derived classes' additional fields won't be preserved, and your object will instead be serialized as if it was the field's declared type. You want to use polymorphism on fields whose type is a custom serializable class.In particular, if you serialize a cyclical graph data structure, you must use SerializeReference to preserve the graph properly and to avoid potential freezes or crashes that can arise from the default serialization approach. This is because the default value-based serialization stores each reference as a separate copy of the object, when fields originally shared a single reference to the same object. You want multiple references to the same instance of a custom serializable class.įor example, reference serialization is necessary for reference-based topologies based on custom serializable classes (such as linked lists, tree structures, or cyclical graphs).You might want to serialize as a reference rather than a value when: ![]() To force Unity to serialize these field types as a reference, use the attribute. In the case of a custom serializable class, this means it serializes only the data belonging to the object assigned to the field, rather than a reference to the object itself. If the field type is one that Unity can automatically serialize by value (simple field types, such as int, string, Vector3, etc), or if it is a custom serializable class or structure marked with the attribute, it is serialized as a value. Fields that reference a UnityEngine.Object like this do not require the SerializeReference attribute, because the serialization for the field always records the reference to an independently-serialized object. For example, a MonoBehaviour that defines a Transform field. If the field type is derived from UnityEngine.Object, Unity serializes it as a reference to that object. UnityEngine.Object fields, by reference:.Without the use of the attribute, Unity serializes each of the fields of an object by value or by reference, depending on the field's type, and according to these serialization rules: ![]() See the serialization manual page for information about serialization and the complete serialization rules. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |