Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Getting a .NET Standard 2.0 project up and running.

Discussion in '2018.2 Beta' started by SGStino, Apr 29, 2018.

  1. SGStino

    SGStino

    Joined:
    Jul 8, 2014
    Posts:
    10
    Hi,

    I've tried to setup a .NET Standard 2.0 project to use unity as it's frontend.
    So far It's been working fairly well, but not without a few bumps in the road.
    However, I'm still stuck at one dependency: System.Numerics.

    Lets start at the beginning:
    I've downloaded the 2018.2 beta through Unity Hub, started a completly new empty Project and changed the player settings:
    upload_2018-4-29_12-56-9.png
    Then I've set up visual studio to copy all dlls from the .NET Standard 2.0 Project to my unity /Assets/References folder.

    And here's where I encountered the first bump:
    upload_2018-4-29_12-59-39.png
    Unloading broken assembly Assets/References/Simulation.Traffic.dll, this assembly can cause crashes in the runtime

    I figured this was the way of unity telling me there's an Type/AssemblyLoadException.
    So what could be the reason for this exception:
    • Actual corrupted assembly file: unlikely because the unit tests work
    • Version mismatch: .NET Standard 2.0 assemblies don't load in older versions of ILSpy, since ILSpy uses Mono to decompile the assemblies, this could be it. But unity's settings are on .NET Standard 2.0, so it should be able to load them with whatever new mechanism/version they use. This couldn't be the reason, could it? So onwards to the next option
    • Typeload exceptions also occur with "or one of it's dependencies" in their message, so could it be that I'm trying to load something that isn't in .NET standard 2.0 itself: I indeed use System.Reactive and System.Numerics.Vectors throughout my project, however, only two of the assemblies cause errors: So what do these two have in common: only System.Reactive.Linq is directly referenced by both of them. So I added the System.Reactive family of assemblies to /Assets/References/Dependencies.
    Problem solved, lets move on to the next:
    When launching Visual Studio 2017 from Assets > Open C# Project I was greeted by this dialog:
    upload_2018-4-29_13-7-58.png
    Since all I did so far in my project was .NET Standard 2.0 and .NET Core unit tests and a .NET Framework 4.6.1 wpf test app i figured, why not: let's change it to 4.6.1.

    Bad idea:
    upload_2018-4-29_13-11-26.png

    So I've downloaded the targeting pack and everything worked!

    Well, "worked". I could open the project in visual studio, write Unity scripts, compile, and hit Play.
    However, what i didn't do was actually instantiate any of my .NET Standard 2.0 assemblies' classes.

    So I started creating a MonoBehaviour to wrap around my own object and render it.
    This is where I needed a reference to System.Reactive.Linq.
    Visual studio didn't recognize that the assembly was referenced by unity, so I had to write the using myself.

    I've hooked up my object to change the GameObject each time one of it's observables called OnNext.
    But one of those observables contained the position as a Vector3 from System.Numerics.Vectors. So I've added that assebly to Assets/References/Dependencies as well.

    But this time, it didn't work as expected:
    upload_2018-4-29_13-57-46.png
    Assets/Scripts/NodeView.cs(29,39): error CS0731: The type forwarder for type `System.Numerics.Vector3' in assembly `System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' has circular dependency


    And type forwarders aren't really a thing I'm experienced with, I know what they are supposed to do, but no idea how they do it.
    What is puzzling me though is that the error mentions System.Numerics.Vector3 in assembly System.Numerics. However, the assembly used is System.Numerics.Vectors.

    To be updated...

    EDIT:
    Great, Turns out I was so silly to copy the System.Numerics.Vectors assembly from my unit test bin folder which is targeting net461. And as such I used the wrong platform version.

    After extracting the netstandard2.0 version from the nuget and restarting unity everything started working.

    I now have a Unity 2018.2 project using .NET Standard 2.0 libraries!
    @unitydevteam: Great job on getting this far!
     
    Last edited: May 1, 2018
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,937
    It looks like you have worked things out. We’re working on some changes to improve this experience end to end. Is there anything still blocking you?
     
  3. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,052
    It will be nice if the error:

    Unloading broken assembly this assembly can cause crashes in the runtime.


    Would return what assemblies dependencies are missing.
     
    FROS7 likes this.
  4. SGStino

    SGStino

    Joined:
    Jul 8, 2014
    Posts:
    10
    Josh,
    For now everything seems to be working. *touches wood*
    This post was more intended on letting you guys know where the rough spots are to polish out.
     
    JoshPeterson likes this.