Article directory
Unity uses Newtonsoft's error reporting solution
Problem Description
Recently, I am doing experiments in the human-computer interaction class, and the main development tool is Unity3D. After uploading the finished project through git , I git clone
used it to test whether the project upload was successful. It turned out that there were some problems with the project returned by clone.
In the project, I used the Newtonsoft.Json package to process the Json data I needed.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
There is no problem in the original project (this is because some things have been set before, and I will mention it in the solution section ), but in the cloned project, the console of Unity3D directly throws an error to me :
The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
This is because Json.NET does not officially support Unity directly, which makes Unity unable to find the correct assembly.
Solution
Below I will give two solutions, both of which are feasible.
Method 1: Use Unity's Package Manager to automatically import
In the Project tab, right-click Packages . Click View in Package Manager
in the menu bar that opens . In the opened Package Manager , click the plus sign in the upper left corner and select Add by name. The package name is com.unity.nuget.newtonsoft-json . When I posted this blog, the version number was 2.0.2 . Readers can also fill in the version number. Unity will prompt you to update after importing. After the addition is successful, Unity's error message disappears.
Method 2: Visit GitHub to download the unitypackage file and import it manually
Visit GitHub to download the corresponding Newtonsoft.Json resource pack that supports Unity. click me to jump
Select the required version to download (because I have used it Newtonsoft.Json.Linq
, so I need to download it JsonNet.9.0.1.unitypackage
).
Open the Unity project that reported the error, double-click the downloaded unitypackage file, and open it with Unity Editor .
Check all packages and click "Import". At this point Unity will recompile the script again.
After the compilation is successful, you will find that there is no error message in the console.