Why Unity includes the Editor directories in build

Viewed 1608

When I try to build my game, Unity includes the scripts contained in my Editor directories. This implies the build to fail because Unity.Editor is not accessible during the build. I'm using Unity 2020.1.6f1, how can I solve this problem ?

This is the type of error i'm getting durring the build:

Assets\Scripts\Editor\EnemyLootEditor.cs(7,32): error CS0246: The type or namespace name 'Editor' could not be found (are you missing a using directive or an assembly reference?)

Theses errors doesn't occur when the game is lauched in Editor Mode.

This is a picture of my Scripts folder that contains all of my code files, including my Editor files:

1

2 Answers

As @Thomas says, yes, you have to check that your Editor files are stored in the Editor directory, and use the compiler directive when you use them.

BUT this problem seems more related with Assembly Definition files, check this related link.

Are you using assembly definition files? They don't respect Editor folders. You need to add Editor asmdefs.

Summarizing, if you have one .asmdef file in your project, and you are using Editor scripts, you need to ALSO have a .asmdef file for your Editor scripts.

Files stored in a folder named "Editor" do not included in builds. (see special folder names in unity)

If you make calls to editor code in "regular" script files you have to surround this code with the UNITY_EDITOR compiler directive:

#if UNITY_EDITOR
// will not be present in build
[...]
#endif
Related