Search Unity

Question Ignore ConditionalAttribute methods on build

Discussion in 'Editor & General Support' started by ChebanovDD, Jan 19, 2023.

  1. ChebanovDD

    ChebanovDD

    Joined:
    Mar 27, 2022
    Posts:
    7
    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.

    Code (CSharp):
    1. [Conditional("UNITY_EDITOR")]
    2. private static void Test()
    3. {
    4.     Debug.Log($"Is any shader compiling: {UnityEditor.ShaderUtil.anythingCompiling}");
    5. }
    I still have to use IF statements to set conditions, even though the method has a conditional attribute.

    Code (CSharp):
    1. [Conditional("UNITY_EDITOR")]
    2. private static void Test()
    3. {
    4. #if UNITY_EDITOR
    5.     Debug.Log($"Is any shader compiling: {UnityEditor.ShaderUtil.anythingCompiling}");
    6. #endif
    7. }
    Why does Unity add support for conditional attributes but it doesn't work as expected?
     
    Last edited: Jan 19, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    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.
     
    chemicalcrux likes this.
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    721
    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!
     
  4. ChebanovDD

    ChebanovDD

    Joined:
    Mar 27, 2022
    Posts:
    7
    So, yeah. The compiler doesn't strip any conditional methods to prevent possible issues with reflection. This makes sense now.
     
    Kurt-Dekker likes this.