- Home /
Unity android application custom dll problem
So I got something really weird going on, I made an application for android, when I start the application in unity everything works fine. However when I build it to my android device it works sometimes. How can a application work sometimes...
Anyhow ofcourse I did some debugging and it seems it sometimes get stuck on a reference that I have on a custom dll imported in the asset folder. I have a server running that sends a List of custom objects to my unity application, in order to deserialize it properly I had to import my server application as dll into unity which I did. Now when I debug it with adb logcat, I can see it sometimes gets stuck on deserializing the object sent from the server but not always. I can start the app like 50 times and like 1 out of 50 it will work.
I have tried the following things:
I have tried to change the API compatibility level from .net 2.0 subset to just .net 2.0
on my dll build I changed the target framework to Unity 3.5 .net full base class libraries
I have debugged my application running it on pc and no errors ever occur
The problem is not the tcpclient or server, because the connection is still there eventho it doesn't receive the custom object properly.
I really about to give up, I don't understand how it works sometimes, other times it doesn't.
------edit------
I should probably mention the only warning I have in unity console is the following:
Serialization depth limit 7 exceeded at 'Android_ChatRoomController::RunServer.lstclients'. There may be an object composition cycle in one or more of your serialized classes.
Android_ChatRoomController is the custom dll.
-----EDIT actual Logcat error:
TypeLoadException: Could not load type 'System.Collections.Generic.List`1[[Android_Cha I/Unity (18811): at System.Type.GetType (System.String typeName, Boolean throwOnError) [0x00000] in :0 I/Unity (18811): at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadTypeMetadata (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo) [0x00000] in :0 I/Unity (18811): at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectInstance (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) [0x00000] in :0 I/Unity (18811): at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) [0x00000] in :0
Since both the warning and the error seem to be related to serialization, I guess that's where the problem is. Never had the problem myself so I don't really know, but from what I could find it seems that there is an issue with what unity serializes.
I think the first post here could be interesting for you:
https://forum.unity3d.com/threads/4-5-serialization-depth.248321/
And if it is indeed the case that private/protected fields are serialized, you would quite feasibly get under a depth of seven. Unity would give up, the serialized object would be invalid and fail would ensue.
Anyhow, if you want to test if this is indeed your issue, you could throw in [NonSerialized] everywhere in your serialized object and then remove them to little by little find out what makes the problem occur.
@nerdclown the warning about serialization depth shouldn't have any influence on the app tho, so I doubt thats the reason, I just now converted the dll target framework from Unity 3.5 .net full base class libraries to .net 2.0, still no change tho, it works sometimes, and other times it doesn't.
I seriously have no idea what is going on, the only thing I know it has to do with the serialization and deserialization.
Well, not knowing your project, which classes you serialize and so on, I still think that could be the issue. I don't see how that warning shouldn't have any influence on the app.
From the post I linked:
Fact #1; Unity stops serializing object after 7 levels of depth. Beyond that, it assumes it's an error - a reference dependency loop. Before 4.5, Unity simply didn't tell you anything and silently erased your data, now it pops an error.
According to your post this exact thing happens, and something is not serialized properly.
Your error call stack shows that the error occurs somewhere in the object reader, which might be the object that was not serialized properly.
Anyhows, if you can make the same problem occurs with a real simple class or everything set to non-serialized, you'll know for sure that I'm mistaken!
This is the class its about
namespace Android_ChatRoomController
{
[Serializable]
class Clients
{
public string ClientName;
[NonSerialized]
public TcpClient ClientSocket;
}
}
I don't understand how to make this better
Answer by Smurfj3 · Mar 16, 2017 at 02:54 PM
Well I got it fixed, I still don't understand why this fixed it but basically what I used to do is have a server application which I turned completely into a dll and imported it into unity so I could deserialize some of the classes in the server application. What I did now, is just made a class library of the classes that actually matter to the unity project and left out the rest of the server application.
So now it seems to work, tried it 3 times and worked every time and the second thing that is awesome about it, is that the dll is smaller :)
Your answer
Follow this Question
Related Questions
How to take a value from Regedit using .NET 3.5? 0 Answers
in Unity 5, how can I get/set an asset's AssetBundle assignment via scripting? 1 Answer
C# assembly and dll files are not being loaded. 2 Answers
Cannot get objects to appear on client screen using new network system on Android wifi 0 Answers
Unity 5.3 In-App Purchase Error 0 Answers