Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question When is UNITY_EDITOR defined and how to avoid defining it?

Discussion in 'Editor & General Support' started by psarrazin, Aug 23, 2022.

  1. psarrazin

    psarrazin

    Joined:
    Jul 20, 2021
    Posts:
    13
    Hi. I'm having trouble with a Unity project where UNITY_EDITOR is defined even though I am launching Unity.exe on the command line. I am not using Unity's graphical environment.

    How can I tell Unity NOT to define UNITY_EDITOR?

    Why is it defined when building outside the actual editor?

    I launch the build this way from a Windows PC:

    Unity.exe -quit -batchmode -nographics -silent-crashes -logFile -projectpath xxx\xxx -executeMethod BuildProcess.PerformBuild -Target Retail -buildPath D:/xxx/xxx/xxx -devmode ON -buildTarget XXX

    In the output, I see this: /define:UNITY_EDITOR

    In the Project Settings, under Player > Other Settings > Scripting Define Symbols, I see no mention of UNITY_EDITOR.

    I'm using 2020.3.26f1.

    Thanks.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    How is impacting the build? I suspect your #if UNITY_EDITOR statements in your code are showing as defined and are trying to compile? Also, please show your PerformBuild method.
     
  3. psarrazin

    psarrazin

    Joined:
    Jul 20, 2021
    Posts:
    13
    Our #if !UNITY_EDITOR statements are not getting compiled, and the statements at the #else are getting compiled instead, which causes compilation errors.

    Here are the relevant parts of PerformBuild():

    Code (CSharp):
    1. public static void PerformBuild()
    2. {
    3.     CommandLine.Init(System.Environment.CommandLine);
    4.     var buildTargetStr = CommandLine.GetString("-buildTarget", string.Empty);
    5.     var buildPath = CommandLine.GetString("-buildPath", string.Empty);
    6.     var targetStr = CommandLine.GetString("-Target", string.Empty);
    7.  
    8.     string optimization = null;
    9.     if (targetStr.ToLower() == "debug" || targetStr.ToLower() == "release" || targetStr.ToLower() == "retail")
    10.         optimization = targetStr;
    11.     else
    12.         optimization = "Release";
    13.  
    14.     BuildTarget buildTarget = BuildTarget.xxx;
    15.     BuildTargetGroup buildTargetGroup = BuildTargetGroup.xxx;
    16.     BuildOptions buildOptions = BuildOptions.Development;
    17.     string exePath = string.Format("{0}/{1}.xxx", buildPath, Application.productName);
    18.  
    19.     if (optimization.ToLower() == "retail")
    20.         PlayerSettings.SetIl2CppCompilerConfiguration(buildTargetGroup, Il2CppCompilerConfiguration.Master);
    21.     else
    22.         PlayerSettings.SetIl2CppCompilerConfiguration(buildTargetGroup, Il2CppCompilerConfiguration.Release);
    23.  
    24.     var buildPlayerOptions = new BuildPlayerOptions
    25.     {
    26.         locationPathName = exePath,
    27.         target = buildTarget,
    28.         targetGroup = buildTargetGroup,
    29.         options = buildOptions,
    30.         scenes = new[] { "Assets/Scenes/SampleScene.unity" }
    31.     };
    32.  
    33.     var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
    34.     [...]
    35. }
    36.  
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @psarrazin Interesting issue, I've been doing a bit of checking. The only other place I've found where these defines are persisted are in .csproj files, something to check anyway. I'm not sure if they are used in the build process or just when using Visual Studio as the code editor. Sorry I can't be of more help.
     
  5. psarrazin

    psarrazin

    Joined:
    Jul 20, 2021
    Posts:
    13
    Thanks for your time.

    I've searched the .csproj files in my project, and these two project files define UNITY_EDITOR:
    • Assembly-CSharp-Editor.csproj
    • Assembly-CSharp.csproj
    I found it suspect that the "non Editor" csproj defines UNITY_EDITOR.

    Manually removing UNITY_EDITOR from Assembly-CSharp.csproj did not affect the situation. Deleting both files also did not affect the situation. They were not regenerated by the next build.

    I don't see any other csproj files that could be related to the Unity project.
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,510
    would
    Code (CSharp):
    1. #undef UNITY_EDITOR
    in your script help?

    (maybe then need to paste that line from commandline build.. otherwise causes issue when inside regular editor?)
     
  7. psarrazin

    psarrazin

    Joined:
    Jul 20, 2021
    Posts:
    13
    Our use of !UNITY_EDITOR is due to DLL problems that arise if we don't use that define on certains parts of the code.

    #undef should be able to let the build succeed, but in general, we will likely get those problems again. (Our temporary workaround is to comment out the "&& !UNITY_EDITOR" subconditions of our #if directives, which achieves the same thing as the #undef in our case.)

    That's why we're looking for the source of the UNITY_EDITOR definition, and for the reason why it's defined all the time, even outside the editor.
     
  8. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    543
    Did you get a clue on this ? I'm having the exact same issue where suddenly a file inside an editor asmdef gets taken into account by the builds, I as well see /define:UNITY_EDITOR in the build log.
     
  9. psarrazin

    psarrazin

    Joined:
    Jul 20, 2021
    Posts:
    13
    I did not get any more info on this. The issue disappeared as mysteriously as it appeared.
     
  10. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    543
    That is so strange, a single class cause the issue, every other classes in the assembly or other editor assemblies have no problem. I'll put the code here in case it does ring a bell to anyone at unity. I removed the class for now.

    @JeffDUnity3D Could it be related to InitializeOnLoad somehow ?

    Code (CSharp):
    1.  
    2. #if UNITY_EDITOR //should not be needed but class added in build for no apparent reason
    3.  
    4. using UnityEditor;
    5. using LibGit2Sharp;
    6.  
    7. namespace Anima.GameEditor.Config
    8. {
    9.     [InitializeOnLoad]
    10.     public static class BlenderInstallScript
    11.     {
    12.         private static string blenderScriptRepo = "https://github.com/builder-main/unity-blender-better-import.git";
    13.         private static string blenderScripFileName = "Unity-BlenderToFBX.py";
    14.         private static string blenderScriptInstallPath = @"Data\Tools";
    15.         private static string backupSuffix = ".back";
    16.  
    17.         static BlenderInstallScript()
    18.         {
    19.             CheckBlenderEnhancedImporterInstallation();
    20.         }
    21.  
    22.         [MenuItem("Indus/" + nameof(CheckBlenderEnhancedImporterInstallation))]
    23.         public static void CheckBlenderEnhancedImporterInstallation()
    24.         {
    25.             EditorUtility.DisplayProgressBar("Loading Blender Enhanced Importer Repo", "", 0);
    26.             var repoPath = Path.Combine(Path.GetTempPath(), "BlenderEnhancedImporter");
    27.  
    28.             if (Directory.Exists(repoPath).Not()) Repository.Clone(blenderScriptRepo, repoPath, new CloneOptions() { });
    29.  
    30.             EditorUtility.DisplayProgressBar("Loading Blender Enhanced Importer Repo", "", 50);
    31.             var repo = new Repository(repoPath);
    32.             Commands.Pull(repo, new Signature("TMP", "TMP", DateTimeOffset.Now), new PullOptions());
    33.  
    34.             var repoFileName = Path.Combine(repoPath, blenderScripFileName);
    35.  
    36.             try
    37.             {
    38.                 var unityEditorPath = Path.GetDirectoryName(EditorApplication.applicationPath);
    39.                 var installFileName = Path.Combine(unityEditorPath, blenderScriptInstallPath, blenderScripFileName);
    40.                 var installedFile = File.ReadAllText(installFileName);
    41.                 var repoFile = File.ReadAllText(repoFileName);
    42.                 if (installedFile.Equals(repoFile))
    43.                 {
    44.                     Debug.Log(
    45.                         $"{installFileName} already match <{blenderScriptRepo}/{blenderScripFileName}> no need to reinstall");
    46.                     return;
    47.                 }
    48.  
    49.                 var backupFileName = installFileName + backupSuffix;
    50.  
    51.                 EditorUtility.ClearProgressBar();
    52.                 var valid = EditorUtility.DisplayDialog(
    53.                     "Blender Enhanced Importer Install",
    54.                     $"{blenderScriptInstallPath} differs from {blenderScriptRepo}/{blenderScripFileName}, would you like to update ? Won't delete previous backup",
    55.                     "Install and Backup", "Cancel");
    56.  
    57.                 if (!valid) return;
    58.  
    59.                 Debug.Log($"Copy <{repoFileName}> to <{installFileName}>, backup if does not exist to <{backupFileName}>");
    60.                 if (File.Exists(backupFileName).Not()) //do not erase backup
    61.                 {
    62.                     File.Copy(installFileName, backupFileName, false);
    63.                 }
    64.  
    65.                 File.Copy(repoFileName, installFileName, true);
    66.  
    67.  
    68.  
    69.             }
    70.             catch (Exception e)
    71.             {
    72.                 Console.WriteLine(e);
    73.                 throw;
    74.             }
    75.             finally
    76.             {
    77.                 EditorUtility.ClearProgressBar();
    78.                 repo.Dispose();
    79.             }
    80.  
    81.  
    82.         }
    83.  
    84.     }
    85. }
    86. #endif
     
  11. sloveys

    sloveys

    Joined:
    Oct 31, 2021
    Posts:
    1
    I've also experienced a similar issue, one script in particular was compiled with UNITY_EDITOR defined within a large project (I can't provide code examples, unfortunately). We think we have identified issues with Unity 2021.3.33f1 (and maybe other versions) occasionally using dirty library files in our project when building Android (and maybe other build targets), so maybe it has to do with the script failing to recompile or compiling with dirty library references to the script?
    I don't have a tun of evidence to support the idea that this is a use of dirty libraries issue so take this with a grain of salt