Overview
I am familiar with using Platform Dependent Compilation in Unity to change how code compiles between editor/standalone/webgl/etc.
I've run into a weird, seemingly trivial case that I can't explain.
The Problem
Given an empty Unity 5.6.1f1 (64bit windows) Project containing just the following script:
using UnityEngine;
public class TestA
{
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoadMethod] // Should be ignored for standalone
#else
[RuntimeInitializeOnLoadMethod]
#endif
static void OnInit() { Debug.Log("TEST A - This does NOT print in Standalone"); }
}
public class TestB
{
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod] // Should be ignored for standalone
#else
[RuntimeInitializeOnLoadMethod]
#endif
static void OnInit() { Debug.Log("TEST B - This DOES print in Standalone"); }
}
public class TestC
{
[RuntimeInitializeOnLoadMethod]
static void OnInit()
{
#if UNITY_EDITOR
Debug.Log("TEST C - UNITY_EDITOR is defined");
#else
Debug.Log("TEST C - UNITY_EDITOR is NOT defined");
#endif
}
}
When compiled and run in-editor I get the output that I would expect:
- Notice how
TestC
shows that, when run in editor, theUNITY_EDITOR
directive is defined as expected.
However when compiled for Window standalone, TestA
fails to print. Here is the relevant content of output_log.txt
after running a standalone build containing only the above script:
- Notice how
TestA
failed to print. - Notice how
TestC
shows that when compiling for Window standalone,UNITY_EDITOR
is not defined (as is expected) and therefore anything in an#if UNITY_EDITOR
section should be ignored by the compiler when compiling for standalone.
So why is there a difference between TestA
and TestB
? Why does TestA
not print in the standalone compilation, but TestB
does?
The only notable difference is that in TestA
the #if UNITY_EDITOR
define has the editor-only runtime init attribute, which is in a namespace that shouldn't even be available for compilation outside the editor (and the compiler would indeed complain if it was not within #if UNITY_EDITOR
).
In other words, When compiling for standalone, only the sections between #else
end #endif
should be used in the compilation, and those two parts are identical in TestA
and TestB
When compiling for standalone, It is my assumption that the above code should behave exactly the same as this code:
using UnityEngine;
public class TestA
{
[RuntimeInitializeOnLoadMethod]
static void OnInit() { Debug.Log("TEST A - This does NOT print in Standalone"); }
}
public class TestB
{
[RuntimeInitializeOnLoadMethod]
static void OnInit() { Debug.Log("TEST B - This DOES print in Standalone"); }
}
public class TestC
{
[RuntimeInitializeOnLoadMethod]
static void OnInit() { Debug.Log("TEST C - UNITY_EDITOR is NOT defined"); }
}
But when compiling this I do see all three lines in output_log.txt:
So something must be different.
What is going on here? Why is TestA
not running in standalone, but TestB
is?
UNITY_EDITOR
is wrapped aroundRuntimeInitializeOnLoadMethod
. File for a bug report.