- Home /
Can a plugin-like system be implemented using AssetBundles?
The original plan was to have a "master" Unity project that would load asset bundles as plugins. For example, the master project would contain the main menu, and the bundles would contain levels, implemented as separate scenes.
That way, adding new levels would not require rebuilding of the player. Simply create the new level in a new scene, pack it into an asset bundle, and put it on the web in a predetermined location (and possibly mention it in some sort of online table of contents). The next run of the master player picks it up and voila - a new level is available.
It seems that this plan folds due to the inability of the bundles to contain code OR scenes, as after playing with asset bundles for a while, I stumbled upon the following error diagnostic: "Asset bundles can not include Scenes".
So the question is, basically, how, if at all? That is, can anyone suggest a mode of operation that would mimic the behaviour described above?
It is highly desirable that the plugins are developed separately from the main player. It is also imperative, obviously, for the resulting scene (as it appears within the master player) to contain some scripts (I am aware of the "Hydra workaround", but perhaps there is a better way?).
Answer by equalsequals · Aug 25, 2010 at 02:58 PM
Any Unity Web Player game I have worked on used separate Scenes as "Asset Bundles" so that the load time was broken up on a per-level basis.
Code to package a scene is here:
_levels[0] = "Assets/Scenes/[Name of Scene].unity";
BuildPipeline.BuildPlayer(_levels, "Build/[Name of Bundle].unity3d", BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
if (System.IO.File.Exists("Build/[Name of Bundle].html"))
System.IO.File.Delete("Build/[Name of Bundle].html");
That is the hard-coded way of doing it, just place that in an Editor script and fire away.
A little setup is involved beforehand though. We have a folder next to our Assets folder in our Unity Project called Build (which is mentioned in the code above) as where the Bundles are stored.
Here is some code for loading the Bundle:
public AssetBundle loadedAsset;
public void LoadAsset(string assetName)
{
StartCoroutine(LoadAssetHelper(assetName));
}
private IEnumerator LoadAssetHelper(string assetName)
{
//because our editor and web paths are different
if (Application.isEditor)
{
assetPath = "file://" + Application.dataPath + "/../Build/" + assetName + ".unity3d";
}
else
{
assetPath = Application.dataPath + "/" + assetName + ".unity3d";
}
www = new WWW(assetPath);
yield return www;
loadedAsset = www.assetBundle;
//good place to place a callback
}
Note: You may need to do some playing around with Application.dataPath in the precursor.
[Edit]
After reading your last comment I got curious as to why it wasn't working for you - so I just put together a quick project with 2 scenes that uses my method. Download the zip here.
You will see when you run the project that Scene1 contains several assets that MainMenu does not, they are all packaged in the bundle and run just fine on load. See the Build folder inside the project for an already working web player publish.
Hope that helps.
==
Interesting. How does one load these scenes? The only way I can think of is redirecting the browser to a new page. That, however, eli$$anonymous$$ates the possibility of using persistent scene objects to track global progress etc.
The second code sample is how you load the Scene via Asset Bundle into memory, from there it is business as usual for loading scenes just as if they were in the build settings: Application.LoadLevel("Scene Name"); Note: This is the Scene name, not the name of the Bundle's file if they were named differently.
Thanks, I already found that. There is a special word for these occasions: RTF$$anonymous$$ :-)
Tried this solution. It doesn't work for the following reason: the created "player" contains only the scene, but not the assets. So if I introduced a new asset, e.g. a material or, more importantly, a script, then it will not be available in the loaded scene.
That's not true, actually. Our scenes have several scripts and assets that are not included in the base scene. Perhaps there is something missing in my explanation that you need to do in the precursor.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072036im_/https://answers.unity.com/themes/thub/images/avi.jpg)