- Home /
Catching DLLImport errors
Hello,
I have a Android and a IOS C# wrapper I use to import my native libraries. I have set them up according to the plugin guides and it works fine.
However, due to the nature of unity I have to reference them both when I build, since I cannot load them at runtime with Reflection on android/IOS unless I manually copy them every time I build and thats not good. This means the C# wrapper for iOS also gets included in Android and vice versa, but I use Application.RuntimePlatform to determine which C# wrapper to use. So it works.
The problem is the declaration in the C# wrappers have to look like this:
[DllImport("__Internal")]
internal static extern void IOS_TestMe();
And if I build on Android, it will throw errors in my log for each and every DllImport used in the iOS wrapper. Is there any way to suppress these errors? I can't use try/catch since its in the class declaration.
Answer by by0log1c · Apr 02, 2012 at 12:39 PM
If I understand your problem correctly, Platform Dependant Compilation might be what you need. It tells the compiler to completely ignore a block of code if its condition is not met, it would not get included in the build.
But I build these C# wrappers in Visual Studio - it's not "free code" lying around in Unity editor, so all pre-processor macros like UNITY_IPHONE_PLAYER gets removed when I build it in VS no?
Sure, when you use precompiled assemblies there's no way to exclude them beside moving them out of your assets folder. If you want to keep your setup, you could move them to an external folder via some editor script.
The other alternative is to wrap the native code functions in a Unity script. If you want a seperate assembly (i'm not sure for what reason) you could also implement a strategy pattern so the external dll get initialized with a concrete interface object that is generated in a script inside Unity.
That's actually a bit strange since every .NET / $$anonymous$$ono assembly as well as all scripts in your project can easily be reflected (disassembled) and viewed with any .NET / CIL reflector. Your code will never be safe. The only reason to use seperate assemblies is to clean up the project a bit and provide a common source which can be used be other external assemblies.