- Home /
Serializing NGUI UIFont - "Material doesn't have a color property '_Color'"
I'm trying to serialize an NGUI UIFont using JsonFX:
UIFont uiFont = (Resources.Load("arial") as GameObject).GetComponent<UIFont>();
string serialized = JsonFx.Json.JsonWriter.Serialize(uiFont);
However, the second line throws two exceptions:
> Material doesn't have a color property
> '_Color'
> System.Reflection.MonoProperty:GetValue(Object,
> Object[])
> JsonFx.Json.JsonWriter:WriteObject(Object,
> Type)
And:
TargetParameterCountException: parameters do not match signature
I know that uiFont is not null (I checked), and this is pretty straightforward use of JxonFx. I'm not sure what would cause this error. Obviously it's something that has to do with reflection, but uiFont.material.color is just a UnityEngine Color. I'm not sure why it's looking for "_Color".
What might cause this issue?
Answer by Bunny83 · Aug 07, 2013 at 04:54 PM
That's simply because the Color property is not a variable. It's a property and it's setter will simply do a SetColor("_Color", value). So it's just a shortcut. Most shaders have a _Color property, but there are a few that don't have one or it's called differently. Like the particle shaders. They have a property called _TintColor but no _Color, so reading or writing the Color property of a shader that doesn't have a _Color will result in an error
That's why it's a bit dangerous to use generic serializes which also work on properties.
Properties are functions and as such they can do quite complex stuff when "used". For example a serializer should never read the material property of a renderer of the mesh property of a meshfilter. The getter of those properties will create a duplicate of the material / mesh. Serializes should always work on the shared materials and shared$$anonymous$$esh properties.
$$anonymous$$gestions for a work-around? JsonFx and NGUI UIFonts are both a requirement for me.
Well, i never used JsonFx yet, but i just had a look on it. I think you can implement a JsonConverter and add it to the writer settings to handle the material type yourself.
Unfortunately as far as i know there's no way to enumerate all shader properties at runtime, so all you can do is test for the most common ones and implement a conditional serialization in your JsonConverter.
Your answer
Follow this Question
Related Questions
usage of FieldInfo.SetValue with SerializeField 2 Answers
Custom Serialization 0 Answers
Method of determining if a field is serialized? 0 Answers
Serialization reflection ExecutionEngineException on iPhone 1 Answer
JsonFX polymorphic deserialization 0 Answers