Search Unity

Question Issue with WebGL compilation?

Discussion in 'Scripting' started by AerionXI, Sep 17, 2022.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    So I'm trying to compile my game with my managed DLL that I compiled into WebGL but I'm getting this error. How do I fix this OR go around it? I still need the code from within the DLL, but can't include the same source file while compiling because the source has the exact same functions ( methods ) as the DLL does. I've attached a screenshot of said error. The DLL works fine and is compiled in VS 2019 Enterprise, with .Net 2.1 Standard Framework and all Unity dependencies.

    weird-build-error.png

    Any help is most-definitely appreciated!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    That error seems to suggest you are trying to use editor code as runtime code possibly, which wouldn't be allowed.

    What are you trying to do exactly?
     
  3. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    How can I either compile this, or get around it?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You may not use UnityEditor methods in built code. End of story.

    If you cannot use CTRL-S to search for your incorrect use of UnityEditor namespace, then you are going to need to bisect to find the problem. Nobody on this board can tell you "File XXX is the problem," you have to find where you inappropriately used UnityEditor.

    If you absolutely MUST use UnityEditor within a script, then guard it with conditional compiles. Here's an example of such a construct:

    https://github.com/kurtdekker/datas...cks/Assets/Datasack/Core/DatasackInspector.cs

    The conditional compilation is controlled via:

    Code (csharp):
    1. #if UNITY_EDITOR
    2. // all of your special editor-only or otherwise non-build code goes here
    3. #endif
     
    Last edited: Sep 19, 2022
    TheDevloper and All_American like this.
  5. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Still can't get it to compile. It still says
    Code (CSharp):
    1.  
    2. UnityEditor.dll assembly is referenced by user code, but this is not allowed
    3.  
    where do I find the problem if I can't even double click on it to reveal the line that is causing the error in the 1st place?
     
    Last edited: Sep 20, 2022
  6. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    how can i track this down if i don't even know what line i'm looking for?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Is this thread from you:

    https://forum.unity.com/threads/issue-using-custom-dll-methods.1333782/#post-8429939

    About making this particular DLL?

    If so, go back to your code and look for and find instances of UnityEditor namespace classes.

    If you absolutely cannot handle this, then start bisecting things: remove HALF the source files, patch over the bleeding. Was that it? If so, cut the remaining in half and repeat, else cut the other half in half.

    https://en.wikipedia.org/wiki/Bisection_(software_engineering)
     
    All_American likes this.
  8. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    540
    I saw a few of your other posts and this might off topic here, but is there any specific reason why you use external dll builds?

    Kurts post already answers the question, but I just want to add one thing, if you use a external dll build then #if UNITY_EDITOR etc will not help you either unless you manually set the variable in your external build project, but then you have to build the dll twice (one with the variable set and one without) and manually use the "correct" dll during editor and build time.

    But I honestly think that you are not "ready" for external dlls if we have to explain all these things, simply move your code to Unity without all the extra stuff and make your life much easier, most of your issues would not exist or would be easier to find if you have your code inside Unity instead of external dlls.
     
    spiney199 likes this.
  9. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    When I tried this, sure the DLL compiles but I cannot exit my game when playing in the Editor even though I have the code for that in the DLL itself.

    Here's the function I call

    Code (CSharp):
    1. public static async void Wait ( float seconds, Action onWaitFinished = null ) {
    2.     // Delay this asynchronous method for x time. ( WILL NOT BLOCK MAIN THREAD )
    3.     await Task.Delay ( TimeSpan.FromSeconds ( seconds  ) );
    4.     onWaitFinished?.Invoke ( );
    5. }
    6.  
    7. public static bool KillApp ( float seconds = 0.0f ) {
    8.     #if UNITY_EDITOR || UNITY_EDITOR_64 || UNITY_EDITOR_WIN || UNITY
    9.         if ( UnityEditor.EditorApplication.isPlaying == true ) {
    10.             killApp = true;
    11.             Time.timeScale = 0;
    12.             Wait ( seconds, ( ) => {
    13.                 UnityEditor.EditorApplication.isPlaying = false;
    14.             } );
    15.         }
    16.     #else
    17.         if ( seconds > 0.0f ) {
    18.             killApp = true;
    19.             Time.timeScale = 0;
    20.             Wait ( seconds, ( ) => {
    21.                 Application.Quit ( );
    22.             } );
    23.         }
    24.         else {
    25.             killApp = true;
    26.             Application.Quit ( );
    27.         }
    28.     #endif
    29.     Debug.Log ( $"killApp : {killApp}" );
    30.     return killApp;
    31. }
    inside of

    Code (CSharp):
    1.  
    2. #if UNITY_EDITOR || UNITY_EDITOR_64 || UNITY_EDITOR_WIN || UNITY
    3.     // KillApp ( )...
    4. #endif
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You really should go read the documentation for Application.Quit().
     
  11. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    What does that have anything to do with

    Code (CSharp):
    1. UnityEditor.EditorApplication.isPlaying = false;
    ?
     
  12. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    540
    Because none of these symbols are set if you manually compile your dll outside of Unity, the compiler will ignore everything inside the #if UNITY_EDITOR || UNITY_EDITOR_64 || UNITY_EDITOR_WIN || UNITY and only compile the else part.
    As explained in my previous answer.
     
    Kurt-Dekker likes this.
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    But is that code even running?

    If that code is in a DLL, those
    #ifdef
    s are not re-evaluated at runtime.

    That DLL is either built with the first part or the second part, as others have explained above.

    Why don't you put different Debug.Log()s in on either side of that
    #else
    so you know positively?

    I gotta just observe that for what this is doing, why is there so much code??!!!!

    At the end of the day, to me it appears you have a static method that;

    - freezes Unity's timescale
    - exits the app after X seconds (default to zero)
    ---> exit means quit in device, stop playing in editor

    That should be just a few lines, MAXIMUM.

    Also, why is it using async / await and Task.Delay()? Why not a coroutine?

    Also, do you know if setting Time.scaleTime to zero impacts Wait()? I don't.

    You have go so many different APIs involved here I'm not really even sure how to reason about the chances of success, except to say "put a Debug.Log() on every line because I'm guessing the code you THINK is running isn't."

    Every line of code you write can have bugs, bugs, bugs. Write less code.
     
    Last edited: Sep 26, 2022
  14. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    How do I set these symbols?
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    These symbols are called conditional compilation directives.

    They only apply when code (C# files) are compiled.

    Once code has been compiled into a DLL, the compilation directives have no effect.

    If you wish to make two versions of the DLL, one for editor and one not, you would need to set the directives appropriately in Visual Studio, compile your two DLLs, then make sure you get the right one when you are either in the editor or building to device.

    This is all just standard "working with DLL" stuff and I'm not even really sure why you are bothering. I have never bothered compiling code to a DLL because it is just so disruptive to workflow and fast iteration, not to mention debugging and other brittleness that it introduces. Are you sure you need to compile to DLL?
     
  16. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    how would i do this in VS 2019 Enterprise?
     
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Are you having trouble with setting the directives or compiling the DLL?

    Screen Shot 2022-09-26 at 11.02.40 AM.png

    Screen Shot 2022-09-26 at 11.02.49 AM.png
     
  18. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    @Kurt-Dekker I am having trouble adding the directives to the DLL. The DLL itself compiles just fine.
     
  19. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You will never "add the directives to the DLL." That's not a thing.

    If you don't understand this, go back to my post #15.

    The directives are set in whatever you are using to create the DLL.

    The directives may affect how code is compiled.

    After that the directives are irrelevant.
     
  20. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    @Kurt-Dekker Ok, so I want to set the directives in VS2019, specifically allowing me to enable #UNITY_EDITOR, #UNITY, etc...
     
  21. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
  22. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    that doesn't help my situation at all. i just want to set the directives for the DLL in vs2019.
     
  23. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,860
    Point is you're over complicating a problem that doesn't need to be this complicated.

    You've still not explained why you supposedly must use an external pre-compiled .dll. You've only posted some extraneously overcomplicated code for quitting an application. Are you trying to modularise your code? There's better ways to do that. It's called packages.

    Honestly any Unity related code should really be native to your project, either in your assets or as a package.

    Don't give yourself a problem that doesn't need to be there.
     
    Kurt-Dekker likes this.
  24. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Keep It Simple Stupid

    You're not going to be able to do what you want to do. Once you build it ....the editor doesn't exist. These guys are experts....they're telling you how it is but you're not accepting it.

    I understand you have libraries you want to use...but you don't need them. it's not like you're using DLL's that were built from C/C++ or something that wouldn't work in Unity.

    Are you trying to put your code into DLL's so no one can hack it? that won't work.....they'll get it if they want anyway.
     
  25. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    that doesn't help my situation at all. i just want to set the directives for the DLL in vs2019.
     
  26. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Cannot you just define them yourself? Granted, you would need to build multiple versions of your DLL. An example from the Microsoft docs:

    Code (CSharp):
    1. #define VC7
    2. //...
    3. #if debug
    4.     Console.WriteLine("Debug build");
    5. #elif VC7
    6.     Console.WriteLine("Visual Studio 7");
    7. #endif
    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives
     
  27. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    This is not a Unity question, it's a Visual Studios question. I've exhausted my ability to help you.

    I don't use VS for anything but its intended purpose under Unity: a text editor and a debugger.

    Beyond that, Visual Studios is dead to me in Unity. VS has no function as a Project manager or as a Solution Manager. Unity recreates those afresh every time you open VS from Unity.
     
  28. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    That is exactly what I am trying to do. Define them myself.
     
  29. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    So now you know, correct? Sorry I didn't quite follow your response.
     
  30. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    I am trying to get Unity to recognize that the #UNITY_EDITOR Preprocessing directives are inside the DLL.
     
  31. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,860
    That's... not how it works.

    By the time it's a .dll it's been compiled. The processors don't do anything after the fact and they aren't a part of the compiled code.
     
    Kurt-Dekker and Spy-Master like this.
  32. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I'm not sure what you mean by "get Unity to recognize...". It won't, just define them yourself, and create separate DLL's, one for the Editor, and the other for your target platform.

    In your DLL source code:

    #define MY_UNITY_EDITOR

    #if MY_UNITY_EDITOR
    // safe to use editor methods
    #elif
    // not safe
    #endif
     
  33. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    So what I am trying to do is simple. I want my DLL to have the code to exit the game when playing in editor play mode, and when I go to hit the escape key, it detects the preprocessor directives and stops the editor play mode from playing the game.
     
  34. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,860
    Oh. My. GOD.

    You genuinely don't understand how this works. At. All. Despite 3-4 people trying to explain otherwise.
     
    Spy-Master and Kurt-Dekker like this.
  35. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Correct, my suggestion would work exactly like this. When your game is running in the Editor and calls your KillApp method, it would call the the one in your DLL that uses UnityEditor.EditorApplication.isPlaying. Then when you build your app outside the Editor, swap out the DLL. Now when you call your KillApp method, it will call Application.Quit. In theory anyway, I've not done this myself.
     
  36. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    ok, here's what i have at the top of my DLL.

    Code (CSharp):
    1. #define UNITY_EDITOR
    2. #define UNITY_EDITOR_64
    3. #define UNITY_EDITOR_WIN
    4. #define UNITY
    5. using System;
    6. using System.IO;
    7. using System.Collections;
    8. using System.Collections.Generic;
    9. using System.Reflection;
    10. using System.Threading.Tasks;
    11. using UnityEngine;
    12. #if UNITY_EDITOR || UNITY_EDITOR_64 || UNITY_EDITOR_WIN || UNITY
    13.     using UnityEditor;
    14. #endif
    but i am getting

    Code (CSharp):
    1. Error CS0234 The type or namespace name 'EditorApplication' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)