• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by pateras · Aug 07, 2013 at 04:42 PM · serializationreflectionuifontjsonfx

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 07, 2013 at 05:02 PM 0
Share

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.

avatar image pateras · Aug 07, 2013 at 05:10 PM 0
Share

$$anonymous$$gestions for a work-around? JsonFx and NGUI UIFonts are both a requirement for me.

avatar image Bunny83 · Aug 07, 2013 at 06:35 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity custom editor when i modify byteArray field with reflection whitout SerializedProperty, it can not save. 1 Answer

How to serialize and deserialize components at runtime? 1 Answer

SerializedProperty and Reflection 1 Answer

usage of FieldInfo.SetValue with SerializeField 2 Answers

Method of determining if a field is serialized? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges