Serialize FieldInfo and PropertyInfo

I am using FieldInfo and PropertyInfo variables. I need to serialize them, I tried:

[SerializeField]
public PropertyInfo propertyInfo;
[SerializeField]
public FieldInfo fieldInfo;

But the data is being lost when Unity exits. How can we serialize FieldInfo and PropertyInfo?

You might want to check the official script reference, which briefly details the types Unity is able to serialize.

You may be able to use other serialization solutions. JsonFx and protobuf-net seem to be fairly popular, but you should definitely spend some time researching something that will work for your project’s needs. There are plenty of threads discussing these, if you search around.

One caveat if you’re building for Apple products: iOS doesn’t allow any code which depends on JIT compilation, which will prevent you from using many of the robust serialization solutions available today.

If you really need to use Unity’s built-in serialization, you can try to find ways to break your data down into types Unity can serialize (ints, floats, strings, bytes), and read/write from those. You might be able to use an array of bytes with binary serialization, for example.