Potential improvement to serialisation of materials with custom properties

Discussion and help for Easy Save 3
Post Reply
chrisdjali-wrld3d
Posts: 3
Joined: Wed Jul 06, 2022 1:47 pm

Potential improvement to serialisation of materials with custom properties

Post 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.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Potential improvement to serialisation of materials with custom properties

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
chrisdjali-wrld3d
Posts: 3
Joined: Wed Jul 06, 2022 1:47 pm

Re: Potential improvement to serialisation of materials with custom properties

Post 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.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Potential improvement to serialisation of materials with custom properties

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply