- Home /
Dynamically loading scripts
My company is using Unity to create an MMO. If possible I'd like to split the game up into self contained blocks (with occasional dependancies on each other if necessary) containing assets and code, that can be downloaded by the users. The game can then load the blocks at run time and assemble the world. I've managed this quite well for the assets using asset bundles and LoadLevelAdditive(), but I can't find a way to do the same with scripts.
How can one distribute and dynamically load scripts without using the one monolithic DLL that Unity builds?
Edit: I'm using the standalone Windows player.
I've actually had an example of how to do this lying around for some time - intended for blogging. I'll dust it off, upload it and link it here later.
Answer by AngryAnt · Jan 05, 2010 at 09:15 AM
I've got a blog post on the subject here: Downloading the hydra. The trick is basically this:
- Compile the external scripts to a .net assembly (the example I provide in the blog post also shows how to have this use UnityEngine functionality).
- Download this via the WWW class.
- Load the bytes of it into the runtime as an assembly.
Thanks, AngryAnt. :-) I'll work through this, but this looks to be exactly what I had in $$anonymous$$d. I'll post feedback tomorrow.
I got the compilation working by downloading and installing $$anonymous$$ono as you suggest in the blog post, and managed to execute the external script. I haven't managed to get it working with the $$anonymous$$ono framework that ships with Unity, but that's not a train smash. Thank you. :-)
you have to be careful so no one injects harmful code in your assembly. you should encrypt and sign the packages.
hey, ant , your download link is invaild now, i can look the blog but can't get the source code.
Answer by Ashkan_gc · Jan 05, 2010 at 08:32 AM
you can create an assetbundle containing all of your scripts. then you can get all of them from your assetbundles. you can seporate them by their names. it's a better way because can be used with web player too. i am waiting for angryant's solution but it can work for you. also i should mention scenes contain scripts too. even you can run codes generated at runtime by using a class in mon called "C# compiler as a service" this class can execute C# code strings. the class is about 3MB and can be downloaded from mono's web site.
The asset bundles do not contain the source, but rather the code is already compiled into your build.
It seems from the forums and my experimentation that asset bundles only contain references to the compiled scripts. The actual script is stored on the project itself. If I add a prefab that references a script to an asset bundle, and then delete the script from the project, the script no longer executes when I import the asset bundle (although the rest of the prefab is there). The same applies to scripts in scenes. I looked at $$anonymous$$ono's "compiler as a service" - it could work, but it's going to require a lot of jumping through hoops. I would rather compile a DLL when the asset bundle is created.
Answer by jonas-echterhoff · Jan 04, 2010 at 11:17 AM
If you have a standalone executable, you should be able to download dlls to disk, and dynamically load functions from them using the usual .NET/mono methods (don't actually know what those are, but google should find the answer to that).
If you are using a web player, then why do you need to do this in the first place? You can always just change the code for the main web player when you need to. Yes, i can still see some valid reasons - code download size, or changing code dynamically while playing or possibly the setup of your build pipeline - but if you describe your situation more detailed, maybe that would help coming up with solutions or workarounds.
I should have specified that I'm using the standalone Windows player - I've edited the question. :-)
Loading DLLs at runtime should work well. I'm not sure how to compile only a subset of the Unity scripts in the project into a DLL - I'm not sure how to compile for $$anonymous$$ono, and what parameters to pass to the compiler, or what other steps Unity goes through to create the DLL.
Answer by metmat · Jun 30, 2011 at 11:14 PM
Any reason to suppose that AngryAnts solution for downloading compiled scripts also work on iphone and android apps?
For me it worked on Android without problems, but iOS made some problems yesterday. However, there were native exceptions, co$$anonymous$$g from semaphores. Not sure why (I didn't use any threading explicitly), but I hope I can investigate this further today.
This will not work on iOS as the platform license prohibits execution of arbitrary code. Additionally, because of that restriction, we actually do not have a running JIT compiler, but in s$$anonymous$$d AOT compile everything. Therefore it would also be technically not feasible.
Hi AngryAnt. I'm sure you understand this better than me, but could you get mono to compile AOT to a .so file (http://www.mono-project.com/AOT) and then use the so ins$$anonymous$$d of a dll?
Hi again AngryAnt. Has this iOS restriction changed now that Apple offers In-App Purchases? According to https://developer.apple.com/news/pdf/in_app_purchase.pdf "In-App Purchase will allow you to sell: ... Additional game levels". Does that mean the In-App Purchase can introduce new functionality by introducing new code? Or is it only able to unlock existing code?
Your answer
Follow this Question
Related Questions
How to import the object from server to unity 2 Answers
Components Inside A DLL 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Build from script 3 Answers
Export list of prefab to asset bundles recursively 0 Answers