Hi all,
Within the editor, I am able to download a .dll using WWW, and instantiate classes from it with Assembly.CreateInstance(…).
When going to the web player, I see the following error in the log (my class/method names scrubbed to protect the innocent :)):
MethodAccessException: Attempt to access a private/protected method failed.
at System.Security.SecurityManager.ThrowException (System.Exception ex) [0x00000] in <filename unknown>:0
at MyClass.myMethod (.Game game) [0x00000] in <filename unknown>:0
at StartLevel.Update () [0x00000] in <filename unknown>:0
Is this possible in the web player? Is there a different/better method?
Here is the solution that is working for me:
-
Download and load your Assembly with WWW
-
Use the following code to load and instantiate a class from within that Assembly:
Type myType = myAssembly.GetType(myTypeName);
ConstructorInfo ci = myType.GetConstructor(new Type{Type.GetType(“MyConstructorArgTypeName”)});
MyClass instance = (MyClass)ci.Invoke(new object{myArg});
(Sorry, I can’t seem to get that to format as code for some reason with leading spaces)
Hmm... reading from http://unity3d.com/support/documentation/Manual/Security%20Sandbox.html, it appears that the WebPlayer sandbox disallows "the usage of System.Reflection.* to call private/internal methods in classes you did not write yourself." In this case, the code in the .dll is internally-developed code, it's just the content-production pipeline for how that is generated takes place outside of Unity3D, and would be rather inefficient to gate through a Unity3D developer to package it up. Is it possible to sign or otherwise indicate "ownership" of the contents of the Assembly/.dll ?
– anon57420914Sorry for the rapid fire... even more strange, I've just been reading through the WebPlayer allowable methods on http://unity3d.com/support/documentation/ScriptReference/MonoCompatibility.html, and it appears some versions of CreateInstance are allowed, but the one I would prefer (that calls a specific constructor, based on an array of object[] args), is disallowed.
– anon57420914