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?
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.
Our #if **!**UNITY_EDITOR statements are not getting compiled, and the statements at the #else are getting compiled instead, which causes compilation errors.
@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.
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.
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.
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.
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 ?
#if UNITY_EDITOR //should not be needed but class added in build for no apparent reason
using UnityEditor;
using LibGit2Sharp;
namespace Anima.GameEditor.Config
{
[InitializeOnLoad]
public static class BlenderInstallScript
{
private static string blenderScriptRepo = "https://github.com/builder-main/unity-blender-better-import.git";
private static string blenderScripFileName = "Unity-BlenderToFBX.py";
private static string blenderScriptInstallPath = @"Data\Tools";
private static string backupSuffix = ".back";
static BlenderInstallScript()
{
CheckBlenderEnhancedImporterInstallation();
}
[MenuItem("Indus/" + nameof(CheckBlenderEnhancedImporterInstallation))]
public static void CheckBlenderEnhancedImporterInstallation()
{
EditorUtility.DisplayProgressBar("Loading Blender Enhanced Importer Repo", "", 0);
var repoPath = Path.Combine(Path.GetTempPath(), "BlenderEnhancedImporter");
if (Directory.Exists(repoPath).Not()) Repository.Clone(blenderScriptRepo, repoPath, new CloneOptions() { });
EditorUtility.DisplayProgressBar("Loading Blender Enhanced Importer Repo", "", 50);
var repo = new Repository(repoPath);
Commands.Pull(repo, new Signature("TMP", "TMP", DateTimeOffset.Now), new PullOptions());
var repoFileName = Path.Combine(repoPath, blenderScripFileName);
try
{
var unityEditorPath = Path.GetDirectoryName(EditorApplication.applicationPath);
var installFileName = Path.Combine(unityEditorPath, blenderScriptInstallPath, blenderScripFileName);
var installedFile = File.ReadAllText(installFileName);
var repoFile = File.ReadAllText(repoFileName);
if (installedFile.Equals(repoFile))
{
Debug.Log(
$"{installFileName} already match <{blenderScriptRepo}/{blenderScripFileName}> no need to reinstall");
return;
}
var backupFileName = installFileName + backupSuffix;
EditorUtility.ClearProgressBar();
var valid = EditorUtility.DisplayDialog(
"Blender Enhanced Importer Install",
$"{blenderScriptInstallPath} differs from {blenderScriptRepo}/{blenderScripFileName}, would you like to update ? Won't delete previous backup",
"Install and Backup", "Cancel");
if (!valid) return;
Debug.Log($"Copy <{repoFileName}> to <{installFileName}>, backup if does not exist to <{backupFileName}>");
if (File.Exists(backupFileName).Not()) //do not erase backup
{
File.Copy(installFileName, backupFileName, false);
}
File.Copy(repoFileName, installFileName, true);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
finally
{
EditorUtility.ClearProgressBar();
repo.Dispose();
}
}
}
}
#endif
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