Search Unity

JSON .NET for Unity

Discussion in 'Assets and Asset Store' started by Dustin-Horne, Sep 13, 2013.

  1. Felix-Schlitter

    Felix-Schlitter

    Joined:
    Sep 30, 2013
    Posts:
    8
    I cannot disclose that stuff, but essentially, the datatypes are:
    string, int, bool, List<Enum>, serializable class, List<serializable class>, List<int>, Enum
    That shoud cover them all. The serializable class type in turn contains the same types and not more.

    It happens exclusively on iOS. It works fine every where, it also works in Unity on Mac. But on iOS it throws a fit. The only reason I purchased this plugin was because the built-in serializer would not work on iOS.
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I don't need your production code, but if you can provide me code that reproduces the error that would be great. I'll try to reproduce it with the information you've given me but it's pretty vague and I can't make any guarantees.
     
  3. Felix-Schlitter

    Felix-Schlitter

    Joined:
    Sep 30, 2013
    Posts:
    8
    I send you a pm with some code. The outbox tells me otherwise, so please let me know if you got it :)
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Got it, sending you a reply now.
     
  5. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    We may have this resolved. I believe it was another fun issue with iOS enumerating collections. Replaced some more enumeration with the AOT safe version and we will be testing in the morning.
     
  6. OSG-Bart

    OSG-Bart

    Joined:
    Nov 12, 2012
    Posts:
    4
    Hi,

    I've just stumbled upon the same problem as PfcGarcia.

    The suggested solution works only if the object assigned to Entity is not an Enum.

    Currently, I can get around this problem using JsonConvert.DeserializeObject(string value, Type type, JsonSerializerSettings settings) , as I can retrieve the expected type before deserialization.

    Unfortunately, as Entity can be of any type present in the loaded assemblies, I have no guaranty that somewhere deep in the serialized object you'll never find a member of type System.Object, which has got an enum assigned to.

    I've tried to solve this using a Converter, trying to serialize the enum's type name and its value, but it seems that the contract applied at read time is the one of the expected type (in this case, System.Object), therefore the Read function of the converter is never called.

    I even tried to trick Json by adding a $type property while writing the object, but I got some errors later in the serialization process (Token String in state Object would result in an invalid JavaScript object.).

    I'm quite new at using Json.NET (got it yesterday afternoon), maybe you have some suggestion? I have the sneaky suspicion that I might have to change the way Json.NET deals with enums if the problem arises, but I'd rather not change third party code.
     
  7. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Eulbart -

    The JObject issue has been fixed and is in version 1.3.2 but is not yet available on the asset store. I'm working on an additional fix for Felix that will also be included (and it will be released to the asset store as version 1.3.3).

    Send me a PM with your Skype contact info if you have it and I will send you the updated 1.3.2 package that includes the JObject fix and you won't have to do anything special to work around it.
     
  8. OSG-Bart

    OSG-Bart

    Joined:
    Nov 12, 2012
    Posts:
    4
    Nice! No hurry though. If the problem occurs before the new version is made available, I'll send you my contact infos.

    Thanks.
     
  9. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Sounds good. :) I'll be testing the other fix today. But, 1.3.2 is complete so if you need it just let me know.
     
  10. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    The fix for Felix's problem has been confirmed. There was a problem serializing a List<enum> on iOS which was caused by an internal LINQ call to SingleOrDefault(). The code has been replaced with an AOT safe method and will be included in the 1.3.3 release which will go to the Asset Store soon.
     
  11. Felix-Schlitter

    Felix-Schlitter

    Joined:
    Sep 30, 2013
    Posts:
    8
    Yes thanks to tripple-A customer support we are that much closer to getting our game out the door on time. It's working like a charm now, thanks so much for your efforts, Dustin.
     
  12. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Thanks Felix. :) Support is something I work particularly hard at and definitely want it to be a good experience.
     
  13. AdamP83

    AdamP83

    Joined:
    Apr 19, 2013
    Posts:
    1
    Hi Dustin,

    I've used LitJSON successfully in previous projects on iPhone and Webplayer, but the next project is going to be a big undertaking so I'm considering switching to something a little more robust and well-maintained. The support you're giving in this thread is encouraging. Is there someplace I could see documentation or code samples to get a better idea of the functionality and usability? I've never used Json.NET, is its documentation EXACTLY relevant to your port? I'd prefer to hear about your product straight from the horse's mouth if possible, but your website lists it as "keep watching".

    Thanks!
     
  14. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Yeah I haven't had much time to work on the website. :/ Support is my #1 priority. But yes, I've kept it exactly as the original version. The only differences are the things that Unity doesn't support (such as Entity Framework and System.Data stuff), but I did shim in the WCF contract attributes so you can create WCF compatible classes with it (DataMember, EnumMember and DataContract). The best thing to do if you need a really good idea is to look at the official JSON .NET documentation and code samples.

    The official documentation is here: http://james.newtonking.com/json/help/index.html

    I'd also be happy to answer any specific implementation questions you have. There are a couple of limitations. You cannot use Dictionaries with reference types as keys. For example, you can use Dictionary<string, SomeClass> just fine, but you cannot use Dictionary<SomeClass, string>.

    Anything specific feel free to ask.
     
  15. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Version 1.3.3 has been submitted to the asset store for approval. Those who purchased through FastSpring can download the update immediately. I will let you know when the update has been accepted.
     
  16. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    IMPORTANT ANNOUNCEMENT

    So I have been looking for ways that I can help my community and I've decided my Asset Store profits would be the best way. I've decided that I will be donating 100% of my December 1st through 20th Asset Store sales profits to the Siena/Francis house which is a local homeless shelter here in Omaha, NE.

    http://www.sienafrancis.org/

    I have contacted them regarding the donation and I'm waiting for a reply. Specifically, they do a lot of work with homeless children and I have asked that my donation go toward providing them with a Christmas holiday that they may not otherwise be able to have. But I need your help! If you have been on the fence about purchasing this Asset, now is the time to do it.

    When I make the donation, I will provide a photo of the check and I will also provide a photo of the receipt as proof of donation as well as any correspondence I receive from the Siena/Francis house so you can see the help your purchase has provided for homeless families.

    The window I'm choosing is from December 1st to December 20th so I can cut them a check in time for the holiday. This is my first announcement of the charitable donation, but so far this means that I have $630 for them already this month. I would love to double that and help as many families as we can!
     
  17. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Great news! I had a conference call with the Siena / Francis house and they are of course very grateful. Right now we are at $630 but I pledged a minimum of $1,000. If we don't make it to $1,000 then the extra will come out of my pocket which I don't mind.. but I would love to give them more. 100% of the profit is going to them so if we end up with $10,000 worth of sales... then that entire $10,000 will go to the Siena / Francis House.

    So everyone knows what the money will be used for, they will be using it to purchase the rest of the toys they need for the children in their women and children's shelter as well as to the children of the homeless and needy families they serve. The rest of the money is going to buy hats, boots and gloves for the children and people in need.
     
  18. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Version 1.3.3 has been approved and is on the asset store. In addition, we have now raised $661.50 for charity!
     
  19. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Thanks to the awesome folks in the Asset Store for working to get my pricing updated! JSON .NET For Unity is now marked down to $25 (that's 44% off) for the charity drive. This should make it more affordable for everyone. We are now up to $696.50 for charity!
     
  20. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,462
    Very Cool Thing you are doing.

    Just bought.
     
  21. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Thank you! But it's not me... it's all of you that help by buying JSON .NET. It's your purchases that are providing Christmas gifts, boots, gloves and hats for these families and children. I can't tell you how much I appreciate it.

    And we are now at $731.50
     
  22. VolureDarkAngel

    VolureDarkAngel

    Joined:
    Jun 27, 2013
    Posts:
    10
    I will be adding another 25 today. After reading your customer support I am both pleased and impressed. I was using the json.net dll's for my win and mac versions, but only recently tried to put it on the web. At which point it was a catastrophic failure.

    Looking over these comments makes me quite comfortable with purchasing this addon. I believe I speak for myself and the rest of the community when I say that we appreciate the extra effort on the customer support.
     
  23. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Thanks Volure! Honestly though, it's the support that this community provides that motivates me to make sure I pay it back. These forums are a rich source of information and a genuinely tight community.

    And while I'm sure the price drop had a little to do with it... the volume of sales today alone which is going to a great charity is very encouraging! We are now at $924 and still going. I've raised $300 just today and I couldn't have done it without all of you. Christmas is a time of giving and I'm super excited to see the good it will do, even if you don't believe in Christmas. In fact, I'm not a religious man myself but I still find great value in it.

    And as always, if you do run into any issues certainly let me know and I'll jump right on it!

    EDIT: Make that $941! We're almost at the goal and I hope to raise much more!

    EDIT #2: $959.00 now... original goal was $1,000 and I am super excited that we're going to well exceed that! Since I was originally going to pay the difference out of pocket, I am still going to match the first $200... so in addition to all of the profits, another $200 of my own money will go to the Siena Francis House!
     
    Last edited: Dec 12, 2013
  24. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I should also mention that Graham Dunnett and Caitlyn went to great lengths to help me get the sale price in place and get this charity drive going. They deserve a lot of credit and they certainly show that Unity Tech. is a stand-up company with great employees.
     
  25. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I just got out of a long quarterly meeting and was shocked to find that we are now well beyond the goal and still climbing. You guys are amazing! We are now at $1326.50 for charity! Add my extra $200 and that's a total of $1,526.50 so far that we will be donating and there are still 7 days left! Thank you all!
     
  26. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Overnight sales have brought the donation now to $1,519. With my match that means we now have $1,719 raised for the Siena / Francis House with a week still to go!

    Absolutely Outstanding!
     
  27. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Asset Store Sales now at $1,624.00 so that brings the donation total to $1,824! We should easily pass the $2,000 mark I hope in the next few days which was my second goal. Tim from Siena / Francis went out and did another round of shopping this morning according to his last email. I've been keeping them updated on the new pledge amounts.
     
  28. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    We are one sale away from passing the second fundraising goal of $2,000! The current sales are at $1921.50. With my $200 match that means we are now donating $2,121.50 to charity! And there are 6 days left to go.
     
  29. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Update:

    Sales: $2,131.50
    Charity Contribution: $2,331.50
     
  30. beer_money

    beer_money

    Joined:
    Feb 28, 2013
    Posts:
    8
    Hi Dustin, I get the following error when building on WP8:

    Assets/JsonDotNet/Source/Serialization/JsonTypeReflector.cs(348,16): error CS0103: The name `LateBoundReflectionDelegateFactory' does not exist in the current context

    any ideas?

    (using unity 4.3 + v1.3 of your asset)
     
  31. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I'm looking at it right now to see what I changed. I'll have it fixed up here shortly. :) PM me with your invoice number and email address or Skype contact and I'll send you an update.
     
  32. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    This has been fixed. This was an artifact of the WinRT upgrade. Just a compilation symbol that I missed. If you want to fix it quickly:

    1. Open /Source/Serialization/LateBoundReflectionDelegateFactory.cs

    2. Change the first line of the file to:
    #if !UNITY_WINRT || UNITY_EDITOR || UNITY_WP8

    The " || UNITY_WP8" is missing at the top. This is necessary because the UNITY_WINRT symbol includes both Windows Store Apps and Windows Phone 8.
     
  33. lyndontroy

    lyndontroy

    Joined:
    Dec 12, 2012
    Posts:
    31
    May ask for a quick summary comparing JSON Object (free in Unity store) and JSON .Net?
     
  34. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Good question. I personally haven't used JSON Object, but it's very simple. JSON .NET is suited to automatically converting your classes and data into JSON, while JSON Object is more of a "builder" type tool. JSON Object has some methods for serializing some common classes, but for the most part you build it yourself by creating a new object and manually add fields and values to it which it then creates JSON for. If all you're doing is storing simple key/value pairs then JSON Object is probably fine for you. However, if you want to do something more complex... consider the following scenario. You have a class called "NPC" and you have several NPC's in your scene:

    Code (csharp):
    1.  
    2. public class NPC
    3. {
    4.    public int Damage { get; set; }
    5.    public int Armor { get; set; }
    6.    public bool IsEnemy { get; set; }
    7. }
    8.  
    Now, you store these in a dictionary:

    Code (csharp):
    1.  
    2.  
    3. var gloria = new NCP { Damage = 100,  Armor = 50, IsEnemy = false };
    4. var steve = new NPC { Damage = 40, Armor = 300, IsEnemy = false };
    5. var aotCompiler = new NPC { Damage = 9999, Armor, 9999, IsEnemy = true };
    6.  
    7. var npcList = new Dictionary<string, NPC>();
    8. npcList.Add("Gloria", gloria);
    9. npcList.Add("Steve", steve);
    10. npcList.Add("AotCompiler", aotCompiler);
    11.  
    12.  
    Now, if you wanted to serialize this in JSON Object you would have to use Nested Json Objects to accomplish it:
    http://wiki.unity3d.com/index.php/JSONObject

    With JSON .NET you just use:

    Code (csharp):
    1.  
    2.   var serialized = JsonConvert.SerializeObject(npcList);
    3.  
    And you're done. And it supports IEnumerable<T> as well as Dictionaries / Keyed Collections. It also supports storing the full type values so you can use polymorphism to store base and inherited class instances in the same collection but properly serialize and deserialize them based on their actual type (instead of the collection type) and cast them back later.
     
  35. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    CHARITY UPDATE

    Days Left in Drive: 4
    Current Sales $2726.50

    This means with my $200 match we are almost at $3,000 for total contribution! Great work guys!
     
  36. lyndontroy

    lyndontroy

    Joined:
    Dec 12, 2012
    Posts:
    31
    Thanks for the answer, product, and charity. My biggest issue with this US fascist system is the wealth aggregation and resulting homelessness. Over 5.6 Trillion belongs ONLY to the MANY Billionaires in the USA, while tens-of-Millions are homeless and hungry. The US spent over 1.5 Trillion killing Afghanistan and combatants, transferred absolute control to a private cartel of banks (Federal Reserve, JP Morgan), gives Afghanistan > $85 Billion per year, gave Trillions to pharmaceutical, energy, GE., and other corporate welfare recipients in past two years, and is now reducing welfare, food stamps, and Medicare. The number of hungry and homeless children WILL rise in the next few years because of this derelict and disgusting US fascist system that is neither a democracy nor example of morality for the world to follow. In fact, the US system is leading many other nations down a similar and destructive path..

    So, in short, your effort and donations are an inspiration to all with morality and souls.

     
    Last edited: Dec 16, 2013
  37. Kermitt54

    Kermitt54

    Joined:
    Aug 14, 2012
    Posts:
    9
    I am receiving the error shown below when trying to serialize a prefab game object. Note that I'm trying to serialize the actual prefab in assets, not an instance of it, and I don't need to deserialize it. It appears that it is trying to serialize the rigidbody property, but for some reason Unity is throwing this error instead of just returning a null value.

    I noticed this happens on all the "shortcut" properties for getting at components, so I attempted to write a ContractResolver to make it just skip these properties (assuming that the serializer would include all the components from some other list on the game object anyway). This seemed to work at first, but once I had it skipping all those properties the error reappeared for the rigidbody property again. I suspect this is because it's finding them on some child component and maybe my ContractResolver is missing them for some reason.

    So why isn't this just serializing a null when it encounters these properties, and how can I get it to serialize a whole prefab (including all attached components) successfully?


    MissingComponentException: There is no 'Rigidbody' attached to the "Prefab" game object, but a script is trying to access it.
    You probably need to add a Rigidbody to the game object "Prefab". Or your script needs to check if the component is attached before using it.
    UnityEngine.Rigidbody.get_velocity () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/NewDynamics.cs:559)
    (wrapper dynamic-method) UnityEngine.Rigidbody.Getvelocity (object) <IL 0x00006, 0x00044>
    Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (object) (at Assets/JsonDotNet/Source/Serialization/DynamicValueProvider.cs:102)
    Rethrow as JsonSerializationException: Error getting value from 'velocity' on 'UnityEngine.Rigidbody'.
    Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (System.Object target) (at Assets/JsonDotNet/Source/Serialization/DynamicValueProvider.cs:106)
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContract collectionValueContract) (at Assets/JsonDotNet/Source/Serialization/JsonSerializerInternalWriter.cs:332)
     
  38. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    It's actually not exactly going to be possible to serialize / deserialize any monobehavior or game object. The Unity documentation claims that they return "null" for those properties but the documentation is incorrect. They actually throw an exception. This is done by Unity, not by the JSON Serializer so it's behavior that I can't change. Well.. ok so that's not exactly accurate. Instead of returning null, it seems to return an uninitialized version of the property. So you can say: if(gameObject.rigidbody) and you're fine... but to the serializer, the rigidbody actually exists and as soon as it tries to access a property of that rigidbody, Unity throws an exception. I'm not sure why they didn't just return null.

    Now, in the case of Components, you're running into the same issue. You'll have to make sure that those properties don't get accessed if it's not actually set. This might get tricky since your components will have different properties. Then again... serializing them isn't going to do you a whole lot of good anyway since you can't manually add the components back to the GameObject, so you'd be best storing your settings / information in separate classes or structs and serializing those accordingly. And email Unity and ask them to return null for those properties instead of throwing exceptions. :)
     
  39. Kermitt54

    Kermitt54

    Joined:
    Aug 14, 2012
    Posts:
    9
    Well that's a bummer. I thought this library would serialize a gameobject without issues. Anyway, that all makes sense, so I think I'm on the right track to just skip the properties. I did make some progress with that just after my previous post. I was only skipping those properties when the type was derived from Component but I extended it to also include GameObject and now I have no more rigidbody errors. Unfortunately I do have a couple of new errors caused by similar things (e.g. Material.Color).

    Some clarification of what I'm doing with this...
    As I said I don't ever want to deserialize it. The purpose of serializing is to get a string that essentially describes all the properties of the components. On the next frame I can serialize it again and compare the two strings to determine if any of the components, or properties on the components has changed. This all happens in an editor class marked with InitializeOnLoad. I've also simplified the problem a bit more by only actually serializing a list of the components, and not the game object that contains them (since that's all I really need to track changes on) and I'm skipping the gameObject property.

    At this point I can't see any better solution, and it is kind of working, so I think I'll just keep adding exceptions into my ContractResolver for now and hopefully I can get it to handle a limited set of components that it's likely to encounter.

    One other thought though... Is there some class I can inherit from to override the method that JSON.NET uses to collect the values (e.g. a different version of DynamicValueProvider), and if so, how would I tell the serializer to use it? If that was possible then maybe I could just capture and hide the errors?
     
  40. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Unfortunately not that I know of, though you could always dive in and rewrite the existing one to your specific purpose. :) Your use case does make sense though.
     
  41. vicenterusso

    vicenterusso

    Joined:
    Jan 8, 2013
    Posts:
    130
    Hi,

    First: congratulations on your donation campaign, really, this is nice!

    Second: I'm having this error and *I think* it might be related to the library.


    Code (csharp):
    1. MissingComponentException: There is no 'Rigidbody' attached to the "CameraISO" game object, but a script is trying to access it.
    2. You probably need to add a Rigidbody to the game object "CameraISO". Or your script needs to check if the component is attached before using it.
    3. UnityEngine.Rigidbody.get_velocity () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/NewDynamics.cs:559)
    4. (wrapper dynamic-method) UnityEngine.Rigidbody.Getvelocity (object) <IL 0x00006, 0x00044>
    5. Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (object) (at Assets/JsonDotNet/Source/Serialization/DynamicValueProvider.cs:102)
    6. Rethrow as JsonSerializationException: Error getting value from 'velocity' on 'UnityEngine.Rigidbody'.
    7. Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (System.Object target) (at Assets/JsonDotNet/Source/Serialization/DynamicValueProvider.cs:106)
    8. Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContract collectionValueContract) (at Assets/JsonDotNet/Source/Serialization/JsonSerializerInternalWriter.cs:332)
    9. UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
    10.  
    Here is the code calling:

    Code (csharp):
    1. var inventory = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CInventory>();
    2. var stringSave = JsonConvert.SerializeObject(inventory);
    "CameraISO" is the object with a tag "MainCamera". It's a camera and it doesn`t have a RigidBody. I have tested with an updated version from the AssetStore

    PS: I'm not using Rigidbody anywhere in my whole project...

    Thanks!
     
    Last edited: Dec 17, 2013
  42. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Thanks! We're 2 sales away from breaking $3,000 so this is going to be a really nice gift for the Siena / Francis House. :)

    As to your error, take a look at the previous few posts for a better explanation. It won't be possible to directly serialize a Component or a GameObject (anything that inherits from MonoBehavior) without writing custom code. According to Unity's documentation, properties like rigidbody return "null" if you don't have one attached... the problem is that's not exactly true. They instead return some kind of uninitialized instance.

    The reason this causes a problem is that the serializer checks the "rigidbody" property and something is returned by Unity (even though you don't actually have one attached), so the serializer tries then serialize the properties of rigidbody... but once you try to access those properties, Unity throws an exception. I'm not sure why they actually return something rather than just returning null... and then throw an exception when you try to access a property.

    So, in short, it's not directly possible to serialize anything that inherits from MonoBehavior. Rigidbody isn't the only property you're going to run into problems with... any built in backing property that returns a component (like Color32) is going to throw the same exception.
     
  43. vicenterusso

    vicenterusso

    Joined:
    Jan 8, 2013
    Posts:
    130

    Thank you for your fast reply. So, the best approach would be mirror all values to be saved in a clean (not monobehaviour) class and serialize this one? I'm already doing this in another part of project, just asking to be sure.

    Apart from that, whats the common usage from most developers?
     
  44. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    You're very welcome. And yes that would be one approach. Or you can use non-monobehavior classes to store all of your data and serialize those whole classes and just keep track of which component they go to. The primary use is for serializing pretty much anything that's not a monobehavior...so if you have managers in your scene that have collections of objects... or say you're making an RPG and you have classes to keep track of your attributes, skills, inventory etc... you would serialize those and save them out as part of your saved game. Another usage is for interfacing with the web, so you can serialize data and then use the WWW class to post that to a web server endpoint.
     
  45. Grespon

    Grespon

    Joined:
    Apr 13, 2012
    Posts:
    388
    Strange.. it's on Winter's Sale but doesn't show up in the list...
     
  46. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Kind of... They put it on special sale for me because I'm donating 100% of the proceeds to charity until December 20th so it's marked as sale (and is on sale) but it's not exactly part of the normal Winter Sale which is why it's not in the list. :)

    By the way... we have passed the $3,000 mark and there's a little over 1 day left to go! I dropped a $3,000 check (picture coming soon) off to the Siena / Francis House over my lunch hour today. Once the drive is over I'll give them another check for the remainder plus my $200 personal match.

    Edit: @Grespon... I've been following your Wet Shaders for awhile by the way and they look really cool, keep up the good work! If I ever pry myself away from coding and get into the graphics side I'll definitely pick them up and try them out.
     
    Last edited: Dec 18, 2013
  47. Reza.mokhtari68

    Reza.mokhtari68

    Joined:
    Oct 4, 2013
    Posts:
    1
    Hi
    i'm beginer in game development, and i use javascript mostly for making my scripts and scene ,
    i was wondering if i can use this asset in my javascript code?

    I have another question about how the reflection is used. Thus reflection has performance penalties, I wanted to know that do you use caching in reflection or just use reflection for every serialization?

    Thank you very much
     
    Last edited: Dec 19, 2013
  48. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Good question on the JavaScript... I'll have to test it out in the morning and see.

    As for Reflection, yes caching is used in a few places. There are some methods and things that need to be reflected, but there is an internal type cache that takes care of the heavy lifting from the type resolution.
     
  49. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I'd have to take a look at a repro, but my best guess is that someone in one of your other 3rd party assets has overrode the System.Reflection.Asssembly type.
     
  50. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Good good, glad you found it. There's nothing wrong with using those generic names... just have to be careful when they're in the global namespace. I try to namespace everything in Unity, unfortunately there are some things you can't in order to stay compatible with 3.5 I believe, like MonoBehaviours. As long as it's not conflicting with a built in type you're ok.

    The reason I jumped right to that conclusion is that I've seen this happen before. There's another third party asset... Soomla I think it's called... and they decided to create a class called Nullable in the global namespace. The problem is that this conflicts with the built in Nullable class. When you say something like:

    public int? Foo

    The question mark is actually a shortcut for Nullable<int>. So when folks were using the Soomla asset, Serialization was breaking because it couldn't resolve which version of Nullable to use. I emailed the Soomla folks about it but never got a response.