Ignore ConditionalAttribute methods on build

Hello. I have a question about the build pipeline. I’m not sure whether it is a bug or not. But the following code won’t build.

[Conditional("UNITY_EDITOR")]
private static void Test()
{
    Debug.Log($"Is any shader compiling: {UnityEditor.ShaderUtil.anythingCompiling}");
}

I still have to use IF statements to set conditions, even though the method has a conditional attribute.

[Conditional("UNITY_EDITOR")]
private static void Test()
{
#if UNITY_EDITOR
    Debug.Log($"Is any shader compiling: {UnityEditor.ShaderUtil.anythingCompiling}");
#endif
}

Why does Unity add support for conditional attributes but it doesn’t work as expected?

I don’t think the problem is in the conditional decorator. I don’t think a decorator could ever prevent underlying code from being compiled.

When building a game, 100% of what is in the UnityEditor namespace simply doesn’t exist, so you would need to guard against using anything like that.

1 Like

Yeah, all that ConditionalAttribute does is tell the compiler to not actually generate the instructions that call the function.

Hence why it only works on functions with a void return type – trying to do that with a non-void function would leave you with a missing value!

So, yeah. The compiler doesn’t strip any conditional methods to prevent possible issues with reflection. This makes sense now.

1 Like