Search Unity

Bug Entities.ForEach in a dll won't build (Solution Posted)

Discussion in 'Entity Component System' started by NT_Ninetails, Dec 23, 2021.

  1. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    It's very strange....When I build a scene with the scripts in the project, it builds and everything is fine. But if I use a dll (compiled from the same scripts using an assembly definition) it will still build as long as any form of Entities.ForEach() is NOT in the script. I get this error:
    Code (CSharp):
    1.  
    2. ArgumentException: The Assembly UnityEditor.CoreModule is referenced by [insert dll here]
    My code has no mention of use for editor code. I have also narrowed down the issue to Entities.ForEach() through process of elimination. I stripped the Entities.ForEach() code to a simple declaration and it still throws the error. It only when Entities.ForEach() is removed will the scene compile with the dll file.

    Code (CSharp):
    1.  
    2. protected override void OnUpdate()
    3. {
    4.  
    5.     TranslationOverrideJobEntityCount = ParticleTransformOverrideQuery.CalculateEntityCount();
    6.     if (TranslationOverrideJobEntityCount > 0)
    7.     {
    8.  
    9.         var GetLTW = GetComponentDataFromEntity<LocalToWorld>();
    10.         var GetTranslation = GetComponentDataFromEntity<Translation>();
    11.         var GetRotation = GetComponentDataFromEntity<Rotation>();
    12.  
    13.         if (TranslationOverrideJobEntityCount > 0)
    14.         {
    15. //doesn't work
    16.             Entities
    17.                 .ForEach((Entity e) =>
    18.                 {
    19.                
    20.                 })
    21.                 .Run();
    22.         }
    23.  
    24.     }
    25. }
    Code (CSharp):
    1.  
    2. protected override void OnUpdate()
    3. {
    4.  
    5.     TranslationOverrideJobEntityCount = ParticleTransformOverrideQuery.CalculateEntityCount();
    6.     if (TranslationOverrideJobEntityCount > 0)
    7.     {
    8.  
    9.         var GetLTW = GetComponentDataFromEntity<LocalToWorld>();
    10.         var GetTranslation = GetComponentDataFromEntity<Translation>();
    11.         var GetRotation = GetComponentDataFromEntity<Rotation>();
    12.  
    13.  
    14.         if (TranslationOverrideJobEntityCount > 0)
    15.         {
    16.  
    17.             //works
    18.         }
    19.  
    20.     }
    21. }
    Just to reiterate, The scripts work just find in a build, it's only when the scripts are converted into a dll does the build fail with the previously mentioned error.

    Any idea on why Entities.ForEach() is considered to be referencing UnityEditor.CoreModule when in a dll?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    What version of Unity
     
  3. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    2021.1.28f1
     
  4. DreamersINC

    DreamersINC

    Joined:
    Mar 4, 2015
    Posts:
    131
    Entities is not yet supported in 2021. Are you using the scriptable build pipeline?
     
  5. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    probably not, i'm just using the Build configuration files.
    upload_2021-12-24_22-41-0.png
     
  6. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    ok so after 2 days of debugging I may have narrowed down the issue. I'm currently using Unity 2020.1.9f1 (Specifically using the Unity.Animation.Sample project as a base). After many tests I managed to get an animation subscene to build and work. I also learned that if I vuild with LiveLink in the build configuration then I get the DOTS_INSTANCING_ON error so LiveLink just breaks the build...which doesn't help my debugging. using the normal Win 64 configuration builds but it only tells me 2 important things

    Code (CSharp):
    1. Script attached to 'GameObject' in scene '' is missing or no valid script is attached.
    2. UnityEditor.BuildPipeline:BuildPlayer(BuildPlayerOptions)
    Code (CSharp):
    1. Failed to build EntityScene for 'Assets/DOTSDynamicBone/Resources/Scenes/BuildTest/BuildTestSubScene.unity
    this really doesn't tell me much other than something went wrong and since LiveLink breaks the build....I have to do more debugging manually/annoyingly....

    From what I can gather, I believe that the subscene somehow makes my custom script become missing?
    and since the script is missing the build generates a warning but this still breaks the building of the subscene thus the entityheader cannot be read?

    So now the question is....why/how is my script dissapearing?

    So I just performed a test and everything builds just fine when I remove the IConvertGameObjectToEntity interface in my script. So maybe there's an issue with the conversion?

    I removed all my code and now the subscene works again...yeah more process of elimination. btw I have to rebuild the scene every test.....it's a good time....
    Code (CSharp):
    1.  
    2. // this works.....
    3. public class CustomScript : MonoBehaviour, IConvertGameObjectToEntity
    4.     {
    5.         public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    6.         {
    7.         }
    8.  
    9.         public void Start()
    10.         {
    11.         }
    12.     }
    correction: nothing works but after putting my code back in the subscene now builds but i still get the warning

    Code (CSharp):
    1. Script attached to 'GameObject' in scene '' is missing or no valid script is attached.
    2. UnityEditor.BuildPipeline:BuildPlayer(BuildPlayerOptions)
    So something is causes my script to just dissapear even when its a normal monobehaviour....at least i think it's my script that's missing since it's the only thing that isn't working and no other information is given...

    nevermind, that warning just appears forever now and my scripts and system work on a normal scene build.
    So this is where i'm at:

    - Using the Unity.Animation.Samples enviroment, I am able to build scenes and subscenes with DOTS Animation, cool.
    - I am able to build with the model on the main scene with my scripts and system working. NOTE: the animation breaks due to some reliance on editor stuff (which is why Unity says to put it in a subscene)
    - My scripts and systems cause the build to break (pretty sure) while in a subscene while the animation works.

    So, any ideas why my scripts and system break in a subscene but work just fine in a scene? Does it have to do with IConvertGameObjectToEntity or some conversion thing?
     
  7. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    I may have found the culprit, just need to test a few more builds
     
  8. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    wow.....so in my IComponentData struct, I have a
    Code (CSharp):
    1. public unsafe  Unity.Physics.Collider* collider;
    which will work with no issues in the editor and in a normal scene build but breaks the subscene build.

    Switching to
    Code (CSharp):
    1. public Unity.Physics.PhysicsCollider collider;
    fixes this issue. I guess that's why the pointer is inside the PhysicsCollider....makes sense....I wish there was a warning though....

    Edit: so i successully got the scene and subscene to build with my code and systems. I'm very certain that the culprit was the Unity.Physics.Collider* collider even though it worked in both the Editor and Scene Build. My solution to fix this is to use the Unity.Physics.PhysicsCollider. So with that change everything now builds as expected. This is not related to the dll issue mentioned ealier though.

    Now I will perform some build tests with this new information and see if the generated dll still acts weird....

    IMPORTANT NOTE: I highly recommend clearing the Entities cache(s) before every build.
     
    Last edited: Dec 26, 2021
    andreiagmu likes this.
  9. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    Update: the dll is still claiming it is referencing UnityEditor.CoreModule but if I use my scripts and perform a build, everything is fine.

    idk why a dll that is created using assembly definitions is being flagged as referecing UnityEditor.CoreModule.
     
  10. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    361
    It's been like this from the very beginning of Unity. In the Editor, runtime dlls will always reference UnityEditor.CoreModule so we can easily use UnityEditor code inside runtime scripts. Just make sure to wrap them in this block:
    Code (CSharp):
    1. // RuntimeScript.cs
    2.  
    3. #if UNITY_EDITOR
    4.  
    5. // editor code
    6.  
    7. #endif
    UnityEditor.CoreModule reference will be removed at build time, also the code inside UNITY_EDITOR block.
     
    andreiagmu and DreamersINC like this.
  11. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    Thanks, however I removed all editor code from my scripts and it still gives that error. Specifically when I add Entities.ForEach() to my script.
     
  12. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Try a supported version of Unity?
     
  13. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    2020.1.9f1: UnityEditor.CoreModule error

    2020.3.6f1: UnityEditor.CoreModule error

    2020.3.25f1: UnityEditor.CoreModule error

    next ill try 2019
     
  14. Kmsxkuse

    Kmsxkuse

    Joined:
    Feb 15, 2019
    Posts:
    306
    This is a long stretch but try adding in
    .WithoutBurst()
    before the
    .Run()
    .
     
  15. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    I'll try that
     
  16. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Oh hang on I actually reread the OP. Are you trying to manually compile a dll with entities foreach in it?

    If so that's unlikely to work as you aren't running the code gen.
     
  17. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    Yeah I took the dll from Library > Script Assemblies > MyAssembly.dll

    I put that in Assets > Plugins folder after removing the scripts and Assembly Definition that created MyAssembly.dll

    This allows me to reference my scripts in the Editor but will not compile during the build claiming it is referencing UnityEditor.CoreModule.

    Maybe I'm not supposed to do treat it as a Plugin or create a Plugin a different way?

    Do you think since I uses the dll generated in the sciprt assembly it somehow references the Editor?

    Do you think I should use the dll generated in a Build > Managed > MyAssembly.dll?

    I tried this an unfourtuanlty this did not work either.
     
  18. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    I haven't tried this at all, but when you make a build, there's a special Player Script Assemblies directory that is generated during a build, and these assemblies omit the editor references. That might be what you actually want to copy from.
     
  19. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    lol, I had to use the generated dll that was in Build > Managed > MyAssembly.dll

    It Builds, If I use the Build version of the dll. Thanks @tertle for the CodeGen tip.

    Steps to reproduce:

    1. Create scripts that uses Entities.ForEach() and use a Assembly Definition to put in it's own dll.

    2. Build the scene you use it in (make sure you have no errors and you probably shouldn't use LiveLink if your using DOTS Animation)

    3. After the build is complete remove the scripts and assembly definition from the project.

    4. go to Build > Project_Managed > YourAssembly.dll and copy YourAssembly.dll to your Project's Asset > Plugins folder.

    5. Congrats, you beat the system!

    I'm going to perform some more builds to make sure this works in different versions
     
  20. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    196
    yeah, that seems to be the solution for now. (Sorry I didn't see your post cause my page was not updated)
     
  21. kakashi8841

    kakashi8841

    Joined:
    Apr 27, 2018
    Posts:
    10
    How do you solve this problem now?