Search Unity

Question Turn dictionary into manageable code

Discussion in 'Getting Started' started by ciaodejan, Jul 15, 2021.

  1. ciaodejan

    ciaodejan

    Joined:
    Nov 29, 2020
    Posts:
    36
    In my code I have this long dictionary:
    Code (CSharp):
    1. static Dictionary<string, Dictionary<string, string>> Translations = new Dictionary<string, Dictionary<string, string>>() {
    2.     ["English"] = new Dictionary<string, string>() {
    3.         ["date0"] = "Tuesday, 22nd January 2019",
    4.         ["time0"] = "8:38 PM",
    5.         ["date1"] = "Saturday, 14th September 2019",
    6.         ["time1"] = "5:26 PM",
    7.         ["date2"] = "Saturday, 28th March 2020",
    8.         ["time2"] = "7:52 AM",
    9.         ["date3"] = "Saturday, 28th March 2020",
    10.         ["time3"] = "1:03 PM",
    11.         ...
    12.     },
    13.     ["Italian"] = new Dictionary<string, string>() {
    14.         ["date0"] = "Martedì, 22 gennaio 2019",
    15.         ["time0"] = "20:38",
    16.         ["date1"] = "Sabato, 14 settembre 2019",
    17.         ["time1"] = "17:26",
    18.         ["date2"] = "Sabato, 28 marzo 2020",
    19.         ["time2"] = "7:52",
    20.         ["date3"] = "Sabato, 28 marzo 2020",
    21.         ["time3"] = "13:03",
    22.         ...
    23.     }
    24. }
    As a workaround to a Unity bug (https://issuetracker.unity3d.com/is...lding-a-project-with-a-large-array-of-strings), I have to load data from an external file or encode it as a stream of bytes.

    You can then read the data into an array (or a dictionary, in my case) yourself and avoid the time/memory expended by the C++ compiler.​

    How do I accomplish this task?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Use JSON or GRFON format.
     
  3. ciaodejan

    ciaodejan

    Joined:
    Nov 29, 2020
    Posts:
    36
    I think I need more guidance. Do I make a JSON external file that I would then use to create my dictionary? If so, how do I go from JSON to dictionary? Or should I use JSON in the code? If so, how?
     
  4. ciaodejan

    ciaodejan

    Joined:
    Nov 29, 2020
    Posts:
    36
    Ok, I got it now. I was able to read from an external JSON file and deserialize it to a dictionary using JSON .NET