- Home /
Load new scripts (post-build) through Asset Bundles. Is this even possible?
Here we are to an age old question, but I'm going to re-address it anyway so please bare with me.
The only disappointing limitation on asset bundles that I can find is the inability to include newly created scripts with the objects in the bundle. Unfortunately for anyone who plans to implement any form of unique logic after a build is in for a metaphorical brick wall.
To put this simply, think of a fighting game with a bunch of different characters (like SSBB) there are characters included in the initial build, but then there are downloadable characters as well. And what makes it so good is that though they all may share the same core mechanics (like base classes in unity) they all function pretty unique from one another and they perform different actions that others cannot.
To achieve this in unity, you could inherit from a base class that is already compiled in the build, but ultimately rely on a newly scripted behavior specific to that character. The question is how can you make an operable character like this for an asset bundle?
Since asset bundles don't support new scripts, I researched and found some potential solutions which point to saving the new scripts as .bytes files and using Reflections to load the behavior at runtime. This may work, but everywhere I look seems to gloss over the main details of actually turning a script into a .bytes file in the first place. Ex. https://docs.unity3d.com/412/Documentation/Manual/binarydata.html
And just say that works, how do we even go about doing this in the first place? If we do manage to succeed in loading a new script, wouldn't that remove any public values you may have assigned to the classes and prefab before exporting to the bundle? The whole process seems perplexing at the moment. And unfortunately the implementation of unique characters and abilities is a crucial feature in the game me and my team are working on.
I know that this is not a new issue, and a lot of people have asked this question before (some people managing to find a workaround somehow). What can be done to make this happen? Is it even worthwhile to attempt this or would it be more efficient to just create the new scripts in the original build (excluding the characters and/or items they are attached to [which will still be separate objects in downloadable asset bundles]), and just export a new update (new build of the original game, now including the required scripts) as an update to be installed and then download the asset bundles which will then function properly?
It just seems like the latter will be more taxing considering that every added level will probably use some new form of scripting (even though it may be a very simple script simply for map aesthetics)
I hope the question is clear and that we can find a solution.
I suggest you take a look at the Playmaker plugin. I've used it to do exactly the sort of thing you're describing, adding completely new $$anonymous$$i-games to a published game via asset bundles, with no need to change the published build.
I should perhaps clarify that the published build did need to have the Playmaker code in it, and the stuff I'd written to connect my $$anonymous$$i-games to the game as a whole.
Regarding the last option you mentioned: Rather than rebuild the whole project, for redistribution, use a DLL. Then, you can rebuild just the DLL, and distribute it with the update, overwriting the original DLL file, to replace the behaviors. Obviously, you don't need ALL your code in the dll, just the code you expect to update regularly.
Dlls would be a first for me but definitely worth looking into.
You can probably use this gist by Paul Hayes to import the DLL at runtime: https://gist.github.com/paulhayes/06435c009d8ae2d305cc7d9e2f6cc1b7
However, some studios write their character logic in a higher level interpreted language such as Lua, to which they've exposed specific game framework functions that they've written in C#. It's similar to Bonfire-Boy's Play$$anonymous$$aker suggestion, but it lets you write text-based code ins$$anonymous$$d of using visual scripting if you prefer that. You can include these scripts as text files in your assetbundles, and feed them to the interpreted language at runtime. There are several Lua packages on the Asset Store: https://www.assetstore.unity3d.com/en/#!/search/page=1/sortby=relevance/query=lua
Your answer
Follow this Question
Related Questions
Avoiding Bloom Pop 1 Answer
Enabling disabling objects via raycast 0 Answers
How to create an Animated 2D Line to help the user aim and shoot? 0 Answers
Compiler failed to execute. 1 Answer
How to make a script slow down to a stop 0 Answers