- Home /
Platform #define directives' can't work
Hello : The 'Platform #define directives' like UNTIY_IOS/UNITY_ANDROID can't work when using with c# Class Library which export with .dll file .
public static int calc(int a,int b)
{
#if UNITY_IOS && !UNITY_EDITOR
Debug.Log("calc IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS IOS ");
return a * b;
#elif UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("calc Android Android Android Android Android Android Android Android Android Android Android Android Android ");
return a + b;
#else
return a - b;
Debug.Log("calc Ohter Ohter Ohter Ohter Ohter Ohter Ohter Ohter Ohter Ohter Ohter Ohter Ohter Ohter ");
#endif
}
it always call #else no matter build on android or iOS, can you help me to solve it .
what do you mean by: when using with c# Class Library which export with .dll file
when you just do a build using a default Unity Build this code as stated should work.
I think they mean they are compiling it to a DLL and including it in the project.
Answer by Captain_Pineapple · Sep 12, 2021 at 09:44 AM
Well then, assuming that @logicandchaos 's assumption is correct and you want to compile code to a dll and then include it in your unity project: This can never work
I can only strongly recommend you read into #defines
as you apparently do not completly understand what they do. They are also called "Preprocessor directives", meaning those are evaluated before the code is compiled. So at the point where you create a dll you already compile the code so Unity will not be able to handle any defines as from unitys point of view they do not exist.
And since at compilation time Visual Studio will check if UNITY_IOS, UNITY_ANDROID or UNITY_EDITOR do exist it will come to the conclusion that this is not the case and thus the #else
path is always chosen.
Long story short: this code works as intended but only if you do not precompile it into a dll.
Right, so, making some more assumptions about the OP's use case... perhaps what they're after is actually platform-dependent DLL inclusion.
They'd need to compile a different version of their DLL for each platform (having set the flags themselves appropriately before building). Then they can put them all in the project but use the DLLs' import settings to only include them for the appropriate platform.
Your answer
Follow this Question
Related Questions
Unity donesn't work in Android 9 0 Answers
Vuforia Image Target for mobile object 0 Answers
Remove UNITY_EDITOR define even for in-Editor builds? 2 Answers