calling Encoding.UTF8.GetBytes(JSON) in a dll causes exception, while in src format everything works great.
This is so weird, I can't even explain it.
I've created an application to add some functionality to my unity game. Everything works fine when I ran the same steps from unity. The steps:
Create a class.
Convert it to JSON. (Using Newtonsoft.Json package)
Add the converted class to another class as a member.
convert that class to JSON as well
run Encoding.UTF8.GetBNytes(json)
It works great when I copy the source files and run them via unity(in the editor), but every time I build it alone and add the DLL, unity raises an exception at the Byte convert stage (I know this because I have tried commenting out every part of the code, and that's the only part that's failing)
This is the error -
TypeLoadException: Could not resolve type with token 01000019 (from typeref, class/assembly System.Text.Encoding, System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
Client.CreatePlayer (System.String client, System.String id, System.String type, System.String teamId, System.Single[] location) (at <59a6b6b960504910bd482e9be83660f7>:0)
PlayerController.Start () (at Assets/Scripts/PlayerController.cs:21)
I've tried switching to ASCII but it still failed. What should I do? :(
Answer by przemeklach · Feb 26, 2019 at 12:15 AM
I am also having this problem. Did you find a solution?,I'm also having this problem. Did you find a solution?
Answer by Saikodan · Feb 26, 2019 at 06:08 PM
Not yet. But since it's only for testing my multiplayer msgs, I've used the source of my code, and when I'm closer to production - I'm gonna transfer without JSON but with another serializer(JSON is not efficient and only for testing) so it should be good.
BTW, my guess is that there is something wrong with the runtime, but I'm not sure.
So I figured out the problem in my case. Turns out the Target Framework in my VSCode project was not the same as the one I had setup in Unity. I believe I was using .Net Core and in Unity I had it set to something else.
So the solution was to set my Target Framework in my plugin project to be .NET Standard 2.0 and then in Unity under Edit>Project Settings>Player ensure that in the Configuration section I had set the Scripting Runtime Version to .Net 4.x Equivalent and then Api Compatibility Level to .Net Standard 2.0.
Apparently whenever there is misalignment between versions you get these these 'Could not resolve type with token' kind of errors.
Yup, that was what I thought. I replaced it to .net framework and it works :)
Your answer
Follow this Question
Related Questions
SHA512 JS -> C# 0 Answers
JsonUtility and Arrays - JSON must represent an object type. 0 Answers
Unity Inventory Database ID Error 1 Answer
UnityWebRequest, C# request.accept equivalent? 1 Answer
JSON string to list with Simple JSON 0 Answers