0

I have a .dll (unmanaged code) that I use in Unity. I need to add some functional in my .dll in order to build Unity project for Android devices.

So, I need to use preprocessor functionality such as if in my case

I tried to add it just for test in my .cpp file

DecoderStream::DecoderStream()
{
    debug_in_unity("DecoderStream", "DecoderStream");  // 1 comment

#if UNITY_ANDROID && !UNITY_EDITOR
    debug_in_unity("DecoderStream", "UNITY_ANDROID"); // 2 comment
#elif UNITY_EDITOR
    debug_in_unity("DecoderStream", "UNITY_EDITOR"); // 3 comment
    m_p_tex_decoder = new DecoderTextureFFmpeg();
#else
    debug_in_unity("DecoderStream", "else"); // 4 comment
#endif
    
}

If I compile this code and run it in Unity I see 1 comment and 4 comment I see that preprocessor conditions doesn't work, I mean it doesn't recognize that whether it android or unity editor...

What am I doing wrong?

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • Do you rebuilt in the different environments (whcih you expect to set/unset the switches) and use the resulting dll accordingly? – Yunnosch Oct 05 '20 at 08:05

1 Answers1

3

From here and as the name already suggests

Pre-processors are a way of making text processing with your C program before they are actually compiled.

So if you don't compile your DLL already defining the according directives your code will basically simply result in compiling

DecoderStream::DecoderStream()
{
    debug_in_unity("DecoderStream", "DecoderStream");  // 1 comment

    debug_in_unity("DecoderStream", "else"); // 4 comment 
}

using the directives it "knows" at compile-time. And since a DLL is then already compiled to machine code it doesn't "know" the concept of pre-processors anymore.

So long story short: You'll have to use the pre-processors on Unity side and just implement different methods like

DecoderStream::DecoderStreamAndroid()
{
    debug_in_unity("DecoderStream", "DecoderStream");  // 1 comment

    debug_in_unity("DecoderStream", "UNITY_ANDROID"); // 2 comment
}

DecoderStream::DecoderStreamEditor()
{
    debug_in_unity("DecoderStream", "DecoderStream");  // 1 comment

    debug_in_unity("DecoderStream", "UNITY_EDITOR"); // 3 comment
    m_p_tex_decoder = new DecoderTextureFFmpeg();
}

DecoderStream::DecoderStreamElse()
{
    debug_in_unity("DecoderStream", "DecoderStream");  // 1 comment

    debug_in_unity("DecoderStream", "else"); // 4 comment
}

and call them accordingly on Unity side

#if UNITY_ANDROID && !UNITY_EDITOR
    DecoderStreamAndroid();
#elif UNITY_EDITOR
    DecoderStreamEditor();
#else
    DecoderStreamElse();
#endif

An alternative from here might be the [Conditional] attribute. However, it can not be used for returning a value, only for void methods.

derHugo
  • 83,094
  • 9
  • 75
  • 115