- Home /
how to import script at run time using AssetBundle?
I've read the documentation of AssetBundles, also the documentation about adding scripts in assetbundles. http://docs.unity3d.com/540/Documentation/Manual/AssetBundlesIntro.html http://docs.unity3d.com/540/Documentation/Manual/scriptsinassetbundles.html
the problem is I don't even know how to start.
"AssetBundles can contain scripts as TextAssets but as such they will not be actual executable code. If you want to include code in your AssetBundles that can be executed in your application it needs to be pre-compiled into an assembly and loaded using the Mono Reflection class (Note: Reflection is not available on platforms that use AOT compilation, such as iOS). You can create your assemblies in any normal C# IDE (e.g. Monodevelop, Visual Studio) or any text editor using the mono/.net compilers."
first of all how do we pre-compile into an assemby a script?
did anyone ever tried this? can anyone tell me how you'd manage to work this out?
to be exact I need a Runtime Standalone program that we can import a whole scene including its features and whatnot.
Answer by AngryAnt · Jul 12, 2016 at 12:41 PM
I'm somewhat disappointed that the Unity docs team decided to actually write up documentation on this hack, but not follow it through.
More information on this hack: http://angryant.com/2010/01/05/downloading-the-hydra/
At the bottom of the post you'll find an example bit of command-line for building an assembly out of your scripts. Note that you can do this more easily with something like Visual Studio or MonoDevelop.
The simple example in the post shows how to load the raw assembly as-is from a URL. Not an approach I would recommend. Aside from you probably wanting to keep the assembly in an asset bundle, you should definitely use some simple checksums to guard against man in the middle attacks.
Anywho, for the asset bundle approach, the trick is to save this .dll as a .bytes file in your Assets folder, which then gets imported as a TextAsset for you to store in an asset bundle and load at runtime.
You can find some example code of doing all of this in an automated fashion in my previous "Unity Hacks" talk: https://github.com/AngryAnt/UnityHacks/tree/master/Assets/Logic/Examples/Bundles
The video is available here: https://vimeopro.com/unity3djp/unite-japan-2013-english-sessions/video/64381935
If you want to go for the gold star and fully automate the process of building unique content, including logic, for loading into already distributed runtimes, I did a writeup on that here: http://angryant.com/2014/04/25/Construct/ - though fair warning this is likely overkill for most use cases and not for the faint of heart. I only undertook that expedition because it was required for a contract (they had approval over the post before I published it).
Even if my inputs on the topic are six (eep!), three, and two years old, it should all still work as-is. Though as the documentation page points out, the very nature of your goal means that it will never work on an AOT runtime and with the advance of IL2CPP, it is going to continue to lose relevance.
I hope that this is useful and helps you reach your goal.
sad to say but your project is depreciated. and I can't do anything to it cause its advance as hell XD. I also saw your Japan session video and you really had good quick and snappy hacks and little tricks. your script bundle idea to save as bytes is great. but it won't let me work on your project so I'm gonna have to create my own. I'm gonna follow your idea and see if it works hopefully.. Thanks so much for the detailed answer sir!
hi, @XenonSpher did you find a solution for importing the scripts at runtime .. any help is much appreciated.
Answer by SpiralConDave · Feb 21, 2017 at 01:54 PM
I know this is scary, but could you do an Eval() ?
Your answer
Follow this Question
Related Questions
Accessing AssetBundle at runtime on Android 0 Answers
How to load images/object at runtime and/or create assets server-side? 0 Answers
Create Assetbundles in final build? 0 Answers
How to import the object from server to unity 2 Answers
Level of Detail & AssetBundle streaming at runtime - possible? 0 Answers