Search code examples
c#jsonunity-game-enginejson.netjsonconvert

Newtonsoft.Json works in Unity Editor but not on mobile devices


I am programming a game of questions and answers by categories in Unity. The categories are obtained through a PHP script that returns a JSON text,* when I use this solution in the UnityEditor it works correctly, but when I install the .apk on my mobile device, deserialization does not work*.

The connection to the mysql database and the PHP scripts work correctly because before I log in and it works fine

string json = [
    {   "id_cat":"1",
        "nombre_cat":"DAM",
        "id_cat_padre":"0"
    },
    {   "id_cat":"4",
        "nombre_cat":"ASIR",
        "id_cat_padre":"0"
    },
    {   "id_cat":"5",
        "nombre_cat":"DAW",
        "id_cat_padre":"0"
    }
]

then I convert this string to a List of Categories

lsSubCategorias = JsonConvert.DeserializeObject<List<Categoria>>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

I've put traces in the code and it's right on that line where it stops.

I Installed the Newtonsoft.Json using the NuGet and it appears in the references.

I've also dealt with only one category object instead of a list, but it doesn't work either. And it is not a visualization problem because I have created category objects and created buttons with them.

The problem is that it works in Unity Editor but it doesn't work on my android device

I have the following mistake in my mobile:

Type ERROR: System.PlatformNotSupportedException ToString Error(): Error.ToString()


Solution

  • I Installed the Newtonsoft.Json using the NuGet and it appears in the references.

    Newtonsoft.Json from NuGet is not supported in Unity3d il2cpp targets, like mobile devices. Use a Newtonsoft.Json fork from the asset store, like this one.