I’m new to Unity, trying to write a managed DLL that will host my game’s various services (comm, io etc). Using windows 10 with the latest .net.
When I try to use asynchronous constructs, specifically as async/await and Tasks, I get errors. Async/await causes unity to notify me that the DLL is broken and is unloaded, which subsequently makes the symbols from the DLL invisible to the game scripts, despite being available in the assets folder and in the references section of the unity VS project.
When I use Tasks (from system.threading.Tasks), the code compiles and the unity scripts see my public symbols, but I get a runtime error that System.Threading.Tasks.Task was not found. If I use the same Tasks code inside the unity VS project, it works well.
For now I’ve resorted to the Threads .net api, which does what I want and works well in this setup. However, I would prefer the convenience of async/await and Tasks.
I suspect that I need to use some special libs for these async constructs, but no search that I’ve run produced clear answers. Any help will be appreciated.
can you try to set the API compatability level to .NET 4.x under “Edit/Project Settings/Player/Other Settings”? Can you check if your dll is imported targeting .NET 4.x? If you select the dll in Unity, the inspector should show that information.
Thanks for the quick reply.
The DLL’s inspector says that the DLL targets .NET 4.x.
In the project itself, the target framework is .net core 3.1.
Another project with the same settings is imported with no problem (but doesn’t reference System.Threading.Tasks).
the player settings (API Compatibility level) target .net 4.x
the project targets .net core 3.1 (I also tried .net core 2.0, same result).
The dll is identified by unity as targeting .net 4.x
Again, the dll is
not imported (identified as broken) if it uses async/await
Generates a runtime error upon access to system.threading.tasks.task
It has to be either some issue with creating the dll or importing it, right? Since there isn’t much to configure when importing, I guess something is off with creating the dll. Those are all guesses that are as good as anyones, I am afraid. I have no experience with importing a dll that makes use of async/await. Might also be that it just does not work for some reason we cannot fix.
Can you somehow include system.threading in your dll when building it? Dunno if that’s a good idea.
I did some more digging around.
When I instantiate a System.Threading.Tasks.Task in a C# file included in the unity project, it is taken from mscorlib.dll provided by unity (in my case, under C:\Program Files\Unity\Hub\Editor\2019.2.16f1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit).
I suspect that I need to compile my DLL against the same mscorlib, but despite adding it to my references and setting true in the project file, I get the same errors.
Again, the problem is specifically with System.Threading.Tasks. Other DLLs that I’ve written are loaded and executed in my unity project with no problems.
The type ‘Task’ exists in both ‘Unity.Tasks, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ and ‘netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’ Assembly-CSharp C:\UnityProjects\HCProto - Copy (3)\Assets\UI\UserDialogManager.cs 10 Active
I’m facing this with an older project that I just upgraded to Unity 2021. I have another project with the same code in it that works fine. I can’t figure out what’s different about the project settings that might cause this.
[edit] ok, in my case I had another class in the project named Task, so it was clashing. Like I said, it’s an older project, otherwise I wouldn’t have name the class that. ha
Hey
any one still facing the issue read here
i was facing the same issue, so there are two “task” classes that are conflicting
i resolved by deleting a file in the plugins folder called “unity.task”
and the issue was resolved