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

醉酒当歌 提交于 2021-01-05 14:45:00

问题


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()


回答1:


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.




回答2:


As answered by @DoctorPangloss Newtonsoft.Json has trouble when building with the IL2CPP scripting backend. This is due to Newtonsoft.Json not having any fully AOT-supported builds available.

There are plenty of third-party solutions who delivers Newtonsoft.Json with AOT-support. The linked JSON .NET by ParentElement is a great such solution, however that project has been dead for a couple of years now, and only gives Newtonsoft.Json up to version 8.0.3.

I would suggest looking at my repository, delivering Newtonsoft.Json up to version 12.0.3 (at time of writing) and delivered via the builtin Unity Package Manager: https://github.com/jilleJr/Newtonsoft.Json-for-Unity#readme

To know more, read my post about What even is AOT? from the context of Newtonsoft.Json.



来源:https://stackoverflow.com/questions/59194154/newtonsoft-json-works-in-unity-editor-but-not-on-mobile-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!