Can't run example from Colyseus Unity SDK, got JsonSerializationException (solved)

13 Jul 2022, 20:10

Hi, there's an issue, need guys to help!

platform: WebGL
There has no exception when running in Unity Editor, but has an exception in the browser.

When the client joins the room, the exception shows up:

JsonSerializationException: Unable to deserialize instance of 'Colyseus.Schema.MapSchema`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' because there is no parameterless constructor is defined on type.
  at GameDevWare.Serialization.Metadata.TypeDescription.CreateInstance () [0x00000] in <00000000000000000000000000000000>:0 
Rethrow as SerializationException: Failed to read value for member 'attributes' of 'ExampleNetworkedUser' type.
More detailed information in inner exception.
  at GameDevWare.Serialization.Serializers.ObjectSerializer.DeserializeMembers (GameDevWare.Serialization.IJsonReader reader, GameDevWare.Serialization.IndexedDictionary`2[KeyT,ValueT] container, GameDevWare.Serialization.Serializers.ObjectSerializer& serializerOverride) [0x00000] in <00000000000000000000000000000000>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0
3 Aug 2022, 11:48

Hi!
The Unity SDK updates frequently lately.

  1. download the sdk: https://github.com/colyseus/colyseus-unity-sdk/releases/tag/0.14.18
  2. download the example: https://github.com/colyseus/colyseus-unity-sdk/tree/master/Assets/Colyseus/Runtime/Example~
  3. copy Example~ folder to unity and remove the "~"
  4. build and run to see if all right.
    0_1659527347045_22233cc6-15c7-4b65-8bf1-0b8d856b0176-image.png
    Good luck!
11 Aug 2022, 16:59

Hi @coco ,
I'm glad to see your reply.

But about the issue I encounter is happening in the 0.14.18 SDK.

I had downloaded the https://github.com/colyseus/colyseus-unity-sdk/archive/refs/tags/0.14.18.zip and used its example which is inside Assets\Colyseus\Runtime\Example~.

When the client joins the room(my_room, not lobby), and scene is changed to ExampleScene, the exception shows up...

0_1660235059003_螢幕擷取畫面 2022-08-12 002405.png


I had traced the source code, and found that GameDevWare.Serialization may not be compatible with WebGL, iOS, and IL2CPP.
For the following reasons.
0_1660236251809_螢幕擷取畫面 2022-08-12 004406.png
0_1660236260525_螢幕擷取畫面 2022-08-12 004309.png

When Colyseus Unity SDK serializes message, it will never get the defaultConstructor. So it will always encounter the exception in WebGL, iOS, and IL2CPP build.

Currently I move the code if (AotRuntime || ... after if (defaultConstructor == null) return false; to continue my test.
Although it works fine, but I'm still looking forward the official update.

12 Aug 2022, 12:51

That's true, buddy, that's true.
Let me ask and see what our support says.

13 Aug 2022, 15:08

@wdemo @coco

Having the same issue! Thanks for posting a quick fix.

13 Aug 2022, 19:13

Hi @wdemo,

I'm currently trying to implement your temp solution. How do you alter your packages usually? I tried a few other solutions suggested on the internet and they don't seem to let me get through building without errors.

15 Aug 2022, 07:59

Hi @gurulantern ,

I download the SDK from git instead of the package by Package Manager, and then copy Colyseus folder into the Assets folder of my project and alter the MetadataReflection.cs source code directly.

17 Aug 2022, 18:01

@wdemo

Thanks! I appreciate your response.

29 Dec 2022, 15:03

Should it have been merged into the project?

This was reported months ago.