Serializing a vector

Currently I am using XmlSerializer which serialises a vector3 as:

0.3333333 0.4 0.5

But I would like it to be as

Is it possible to change this?

Not easily. You can get something close to your desire by decorating fields of classes with XmlElement or XmlAttribute attributes (see c# - Serialize Property as Xml Attribute in Element - Stack Overflow). However, the definition of Vector3 is out of our control, so there is no way to modify it.

You could try writing a type converter for it, as described in c# - XmlSerializer property converter - Stack Overflow

With XmlAttribute is not possible since it just converts it to a string which loses precision myvector=(0.3,0.4,0.5) I’ll look at the other way.

Rather than using the built-in serializer, try something like SharpSerializer. Faster, and better output. Of course, since Vector3s aren’t marked as serializable (thanks UT), you’ll then have to roll out your own Vector3-like struct, and interface it with Unity.

1 Like

We have a Vec3String class that serializes the three components as string attributes and has methods to convert to and from a regular Vector3

1 Like