Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Serialization problems (lots of problems)

Discussion in 'Scripting' started by Burton-kun, Nov 21, 2015.

  1. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    I write once again on the forum about the same problem because I didn't get satisfactory answers.
    For my project I'm building a little personal engine in Unity to automate some processes that instead would be expensive.
    Therefore, I'm working on various interfaces among which: Custom Inspectors, Editor Windows etc...
    The point is this: Through these multiple windows and inspectors, I find myself generating and managing lots of variables, lists of variables, classes, lists of classes, arrays, lists of arrays and similars.
    I tried everything in these last months: Serialization, json library, xml, playerPrefs, streamWriter, streamReader. Nothing seems to work. I keep obtaining untranslatable errors or the obtained result is not the hoped one.
    Nobody seems to be able to help me and the forum's guide is not at all clear. So I start from the beginning and I open this thread, hoping to get answers and patience:
    Considering the variables' type and objects above mentioned that I have to keep between edit mode, play mode and after closing and reopening the program as well: What do You suggest? How can I overcome this mangy obstacle? Thank you very much.
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    You can't serialize objects that are not ISerializable or do not implement this interface. So that for starters I hope you know IE classes that inherit from Monobehavior. If you have Dictionaries, this will be a hard thing to serialize in XML. I found this helpful hopefully this helps you. I suggest JSON however as the files tent to be smaller than XML because of formatting etc. Newtonsoft has an awesome JSON DLL that you can use in your project to serialize pretty much anything to JSON including Dictionaries.

    Now you are very vague as to what it is that is wrong or what it is you need...saving date to a file for persistence is always great not sure why that wouldnt work for you. If anything you can always create .assets whill serialize and save your data in Edit and play mode. You can load it from Resources folder Resources.Load("MyAsset") as MyAssetClass;. Again, its very vague so please be specific as to whats the problem.
     
  3. Duffer123

    Duffer123

    Joined:
    May 24, 2015
    Posts:
    1,215
    I have just posted semi-related but re ScriptableObjects. . So far I've found them the best way forward but my post was about how best to implement them. . Even with ScriptableObjects you cannot easily serialise dictionaries. If you're interested in scriptableobject Google that and inventories and you'll quickly get a feel for their massive potential... ;)
     
  4. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    It's funny because after posting this desperate question I found the solution ^^"
    I found an interesting, useful and very clear thread where serialization and following problems are well explained.
    I leave below the link to the thread for the users like me that will have troubles:
    http://blogs.unity3d.com/2012/10/25/unity-serialization/

    Thanks to Polymorphik and Duffer123 for the answers :)