Serialize AssetImporter.userData

Hello all,

I’m working on a tool that has a bunch of meta-data that is only required at edit-time and we don’t want the data to make it into game. It looks like AssetImporter.userData is perfect for this.

My question though; is there any way we can make use of Unity’s serializer for this data? We’re using YAML files for the ability to merge so it would be great if this meta-data was serialized as YAML as well.

My fallback is going to be either using JSON (I’m concerned that this might mess up the parsing of the .meta) or MemoryStream to save the bytes to the string (which means no manual editing of the data, no merges possible) but using Unity’s serializer would be the best option.

A quick look at the documentation yielded nothing promising though, so I’m not holding my breath.

By Unity serializer, do you mean the serializer by Mike Talbot? or Unity’s serialization system?

I don’t think there’s any easy way to do this. I can just about imagine some horrible hack along the lines of “set the project to Force Text mode, create a ScriptableObject subclass with your data in, serialise it to a .asset file, then open the file and read back in the serialised YAML,” but, well… horrible hack.

That said, if you have a lot of data, perhaps you’d be better off storing it in something like a ScriptableObject .asset file (and thus getting automatic serialisation, Inspector support, etc) and then just storing the GUID of the .asset file into the userData field?

Yea, that is a pretty horrible hack but for saving it that would work fairly well, deserializing it would be a different issue. I could rewrite the data back to the disk but at that point I might as well just leave it as an additional scriptable object and store the guid as you suggested. I’ll keep this as my last-resort-before-binary solution.

In the meantime I’m going to give JSON a shot. I believe we’re already using a JSON library for some client-server REST API. Do you think the YAML parser will barf on that? Or will it be able to encapsulate the string in a way that it would be safe?

For completeness and anyone who might stumble across this in the future; JSON works well with the YAML parser, I’m using JsonFx. The string is encapsulated with a single quote and it does escape them nicely if used in your JSON (it’s escaped with a double-').