I’ve been banging my head against a C# coding problem for the last few days.
The problem is this - we're developing a Unity plugin and issuing the code as DLLs for use by game developers. The developers would then plumb code from our DLLs into their Unity programmes by drag-and drop.
So far so good.
Unfortunately one of the compilers that some developers use to build for Android (IL2CPP) breaks if it encounters managed iOS code, and we use some plugin code on iOS and Android to detect hardware capabilites.
Specifically, "IL2CPP requires that all native methods used in managed code be present at link time, where Mono does not,” as quoted in https://forum.unity.com/threads/build-fails-with-il2cpp.523955/ - the native methods in managed code being our iOS plugin in this case.
Now, normally we’d just wrap the code in an #if UNITY_IOS preprocessor directive, job done. But because we’re compiling the code into DLLs to ship, we’d need to make the choice at compile time. Which means that we can either compile a version that works on Android, or a version that works on iOS, but not both.
I’ve tried several routes to get around this, none of which has worked as yet. As soon as there’s a reference to the iOS code (eg in a switch statement to only use it at runtime) the Android compiler will follow the path to the native iOS methods, be unable to parse them, and Error. But if I try stripping out the code with a preprocessor directive, it no longer works on iPhone, because we’ve stripped the code out of the DLL.
I know there must be a solution to this, presumably involving loading the class at runtime, as otherwise how could you create a single package in C# for anything that ran on different types of hardware? But at the moment I still don’t know how to crack it.
↧