When can I use Shader.EnableKeyword()?

I’m trying to use a preprocessor macro in my custom shader so it performs a different step if it’s in the Unity Editor:

#if defined(IS_UNITY_EDITOR)
    // something
#else
    // other thing
#endif

To achieve this, I’m using the Shader.EnableKeyword method in a MonoBehavior:

if (Application.isEditor) {
    Shader.EnableKeyword("IS_UNITY_EDITOR");
}

However, my shader is being compiled before the monobehavior can check if it’s in the unity editor. I’ve tried running it on Start() and on Awake(), but the shader never receives the IS_UNITY_EDITOR definition. How can I enable that keyword before the shader is compiled?

in your shader you will want to make sure that you have a line that says

#pragma multi_compile __ IS_UNITY_EDITOR

so that the shader variation you want is actually compiled so you can call it at runtime.