- Home /
Dynamically loading Assembly in Web Player
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)
Answer by jonas-echterhoff · Sep 09, 2011 at 10:17 AM
While I'm not quite sure why Assembly.CreateInstance would not be allowed in the web player, you should be able to use Assembly.Load. See the "Including scripts in AssetBundles" example here.
Hmm... reading from http://unity3d.com/support/documentation/$$anonymous$$anual/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 ?
Sorry for the rapid fire... even more strange, I've just been reading through the WebPlayer allowable methods on http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoCompatibility.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.
Your answer
Follow this Question
Related Questions
Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers
Using DLLS in a Webplayer 0 Answers
System.CodeDom in Webplayer? 0 Answers
Custom dll on web player 1 Answer
Web build System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded. 2 Answers