- Home /
C# Reflection and Android/IOS
Hello,
Does Reflection truly work on Android/IOS? For example,
Assembly testAssembly = Assembly.LoadFile("Test.dll");
Fails because it cannot find the file specified when I try run it on Android.
Thanks
Answer by Bunny83 · Mar 30, 2012 at 10:27 AM
I guess it should work According to the MonoCompatibility page. Assembly.Load() with a Byte array works even in the webplayer.
I guess your problem is where your file is located. Since you load it manually the file isn't part of your build so you have to copy it manually somewhere from which you can load it. You might want to use one of those directories: Application.dataPath, Application.persistentDataPath or Application.temporaryCachePath
Also keep in mind when you load assemblies with classes derived from MonoBehaviour or Scriptableobject you can't use them in Unity. The class for itself works, but AddComponent will fail since the class is unknown to the Assetdatabase of your project.
It works fine if you use only custom classes (not derived from MonoBehaviour).
Is there Really no way to include the dll when building an app in unity? Asking everyone to manually copy it to a destination on the phone is way too much of a hassle. It seems like quite a limitation, why would they not provide something like that?
I was wondering why you actually want to load it manually :D
.NET / $$anonymous$$ono assemblies can just be dropped anywhere in the assets folder. In this case they are part of the project and can contain also classes derived from $$anonymous$$onoBehaviour.
All your scripts in your project are also compiled into a dll assembly. You never have to load an assembly manually when it's available at building time. You can use it like any other assembly.
A little sidenote: If your dll contains $$anonymous$$onoBehaviour components Unity will "extract" them into the global namespace. The dll-asset in your project view shows contained components ($$anonymous$$onoBehaviours) as sub assets so you can drag&drop them onto GameObjects.
I need to do this because I have pre-built in Visual Studio different DLL's depending on which platform. The DLLs are wrappers for native code on their platform For example, I have one built for PC, which loads PC-native code. Then I have one buitl for Android/iOS, which links against UnityEngine (which the prior must not) to deter$$anonymous$$e platform, then loads their native versions.
$$anonymous$$y hack now is to always have reference to both DLLs even though I am only building for one of the platforms. This creates unnecessary amount of code and as a nasty side-effect will fill my log when the application starts with tons of DLLimport errors
If I was able to load at runtime I could deter$$anonymous$$e which DLL to import depending platform
Well, native code is a different story. Plugins (true native code dlls [pro / mobile only]) just have to be placed in the plugins folder and can be wrapped with a simple DllImport attribute.
To automatically exclude certain code use Platform Dependent Compilation
The DLLs have to be removed or added to the project manually. There's no way i know to exclude an assembly from a build that is placed in the assets folder.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
C# - Reflection to invoke static method freezes unity 0 Answers
Multiple Cars not working 1 Answer
turn on off particle system 1 Answer