Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Emit.DefineDynamicAssembly works in Editor but not in Standalone

Discussion in 'Editor & General Support' started by Maeslezo, Jul 20, 2021.

  1. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    291
    Hello,
    I am integrating pythonnet into Unity and it seems to work pretty well.

    But, when I build a standalone version, I got a PlatformNotSupportedException.

    Taking a closer look, I have found that the exception is triggered by Emit.DefineDynamicAssembly.

    How can it be working on Editor and not in standalone version?

    Maybe @JoshPeterson could have some clue?

    Addition information:
    • Unity 2020.3.11
    • Scripting Backend: Mono
    • Api Compatibility Level: .Net Standard 2.0
    • pythonnet 3.0
    • It works in Unity Editor Net 4
    • It works in Standalone Net 4
    • It works in Unity Editor Net Standard 2
    • It doesn't work in Standalone Net Standard 2.
    Unfortunately, switching to .Net 4.x is not an option

    Thank you
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Which platform are you building for? It looks like Emit.DefineDynamicAssembly is not supported in .NET Standard 2.0: https://apisof.net/catalog/f10250b4-9694-850a-9d5e-d2c0e3808b26

    I'm surprised that it works in the editor when .NET Standard 2.0 is the Api Compatibility option - that seems incorrect.
     
  3. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    291
    Yep, I agree that it is very weird

    I am building against:
    • Target platform: Windows
    • Architecture x86_64
    Debugging the code, in Unity Editor, Emit is loaded from
    C:\Program Files\Unity\2020.3.11f1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit\Facades\System.Reflection.Emit.dll

    upload_2021-7-20_13-26-52.png

    Debugging the Player, Emit is also loaded, this time from (...)\Plugin_Data\Managed\System.Reflection.Emit.dll
    upload_2021-7-20_13-39-6.png

    But I am not sure if they are the same, because if they are the same version, they should work in the same way, in Editor and in Standalone, shouldn't they?

    More info:
    upload_2021-7-20_13-41-55.png

    upload_2021-7-20_13-43-38.png
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Thanks for the details - is the call to DefineDynamicAssembly in C# code that Unity compiles? Or is it in a pre-built assembly?
     
  5. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    291
    It's in a pre-built assembly
     
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Ok, that makes a bit more sense then. What base class library API is that assembly compiled against? I'm guessing that it is not .NET Standard 2.0, since DefineDynamicAssembly is not part of that API, and the assembly would not compile.

    Is it built against .NET Framework 4.8 maybe?
     
  7. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    291
    I'm not sure how to get that information but in Unity Inspector it is marked as .Net 4.x

    upload_2021-7-20_15-1-19.png
     
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Ok, so it is built against .NET 4.(something), so you will need to use the .NET 4.x Api Compatibility Level in Unity in order for it to work. The fact that it works in the Unity editor with the .NET Standard 2.0 Api Compatibility Level is kind of unfortunate. It's not really a bug, but a side effect of the way the implemented of the BCL in Unity is implemented.
     
    Maeslezo likes this.
  9. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    291
    Ok, thank you very much for your help.

    They are actually bad news because I can not change the API Compatibility level.

    it would have been great if Unity Editor and the Standalone version had behaved in the same way.

    Last question, what do you mean with BLC?
     
  10. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Sorry, BCL means base class library - I should have been more clear. This is the API that the C# code compiles against - things like List<T> and DefineDynamicAssembly.
     
  11. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    291
    got it. Thanks
     
  12. alikun

    alikun

    Joined:
    Oct 17, 2018
    Posts:
    16
    Hi,
    @Maeslezo did you find a solution to this?
     
  13. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    291
    No, unfortunately I didn't. The problem was that Editor and Standalone was working different but the "correct" behavior is the standalone one.

    You can see the full development here in case it helps
    https://github.com/Maesla/PythonForUnity
     
  14. alikun

    alikun

    Joined:
    Oct 17, 2018
    Posts:
    16