不,Unity 不会#if UNITY_EDITOR
在构建中包含代码。
您可以使用以下示例对此进行测试:
void Start ()
{
#if UNITY_EDITOR
Debug.Log("Unity Editor");
#else
// note that this block of code is not included while working in the Editor,
// but it will be included when building to Android
// (or any other build target outside the editor)
Debug.Log("Not Unity Editor");
#endif
#if UNITY_ANDROID
Debug.Log("Android");
#endif
#if UNITY_STANDALONE_WIN
Debug.Log("Stand Alone Windows");
// Including a garbage line of code below to show
// that code really isn't included with the build
// when the build target doesn't match, e.g. set to Android
fkldjsalfjdasfkldjsafsdk;
#endif
}
如果您的构建目标设置为 Android,尽管编译错误,您的应用程序仍应构建,因为UNITY_STANDALONE_WIN
代码已完全删除(并且您的 IDE 可能会使代码块变灰)。将构建目标更改为 Windows 后,代码将无法编译。
(就个人而言,我更喜欢尽可能地使用Application.isEditor
over #if UNITY_EDITOR
macro 作为一种习惯,因为使用#if UNITY_EDITOR
with#else
可能会导致您中断构建,直到稍后才意识到任何无法在 #else 块中编译的代码。我通常更担心关于这一点,而不是在我的构建中包含额外的无用代码。当然,在使用UnityEditor
类时,使用#if UNITY_EDITOR
是不可避免的。)