Serialization and deserialization of Unity3D MonoBehaviour script

Reference Information. Classes that inherit from Monobehaviour cannot be serialized.

Premise: a way to save data (variables / fields and their values) in a MonoBehaviour script so that it can be serialized and deserialized again and use this data to "populate" the corresponding MonoBehaviour script variables / field values.

Tried so far:

  • The presence of a serializable shell / container class that has the same fields as the MB script, but does not inherit from MB. Works fine, but each MV script needs its own wrapper class and has its own wrapper function.
  • Serialization List<FieldInfo>and filling it with MB fields ... 30% work;
    • FieldInfos are added but are of the wrong type and
    • When deserializing their values ​​is not available because an instance of the class is needed, but only a list is available

I feel it may not be that difficult, but my Reflection skills and related ones are limited, but seeing that saving / loading is a fairly common feature, I hope there is someone who has done this, or someone who can point me right direction.

+4
source share
2 answers

There is no easy way to serialize MonoBehaviour using the BinaryFormatter built into .NET. There are several options you can consider:

  • Memento Patter. ( ) , . Momento , .
  • Unity, :

    void Serialize() {}

    void Deserialize() {}

    MonoBehaviour script, /, /.

  • , GitHub. , ( ). , Unity3d, , .

+2

: ScriptableObject. .

ScriptableObject, Unity . .

0

All Articles