Page 1 of 1

Potential improvement to serialisation of materials with custom properties

Posted: Mon Jul 18, 2022 5:47 pm
by chrisdjali-wrld3d
Currently the serialisation of UnityEngine.Material calls Material.HasProperty with a bunch of predefined property names, and then when it does indeed have the given property, calls the relevant Material.Get* method for that name's associated type. This isn't particularly flexible as materials with custom shaders can use custom property names and give each property a custom type.

Looking at the API documentation, it appears that while the names and types of properties aren't available via the Material interface, they are available via the Shader interface, so it looks like it would be viable to get the material's shader and iterate over its properties, calling Shader.GetPropertyName and Shader.GetPropertyType Shader.GetPropertyCount times, using a switch to serialise the material's value for the property as the correct type. Similarly, when deserialising, Shader.GetPropertyIndex could be used to get the index required to call Shader.GetPropertyType again to extract the right type from the JSON data.

Re: Potential improvement to serialisation of materials with custom properties

Posted: Tue Jul 19, 2022 9:49 am
by Joel
Hi there, and thanks for getting in contact.

Unless we're mistaken, the Shader functionality is still incomplete and cannot be used to get all properties which have been set on the Material. Specifically the types of variables represented by the Shader properties (see ShaderPropertyType) does not match the types of properties which can be set on the Material, which allows for types such as Matrix and arrays.

All the best,
Joel

Re: Potential improvement to serialisation of materials with custom properties

Posted: Tue Jul 19, 2022 6:14 pm
by chrisdjali-wrld3d
The serialisation already present in Easy Save 3 doesn't deal with any matrix or array properties - the only ones which are serialised that aren't one of those types are ancillary properties of textures which are accessed with the name name as the texture they apply to and therefore implicit - so switching to the system proposed here wouldn't lose anything, but would cover some cases which currently don't work.

Re: Potential improvement to serialisation of materials with custom properties

Posted: Wed Jul 20, 2022 10:16 am
by Joel
This is true, though our previous stance has been to avoid supporting incomplete functionality to prevent unexpected errors between updates of Unity.

We'll get in touch with Unity to see what the likelihood is of this changing and whether it's likely to cause any serialisation issues if so. If not, we'll include it in a future update.

All the best,
Joel