I have a scene that I’m setting up to deploy online and I really want to set up Asset Bundles to store some 3D models. To give an overview of what I need - My scene has an icon that the user can click to open a “model viewer” where they can manipulate 3D assets. I’ve set this up already for standalone using the Resource folder so the fundamentals are already set up no problem. I need to change my code to do the following;
- Asset Bundle to download from an FTP when the scene has loaded and the user is exploring the environment.
- Have assets available so when I open the model viewer an asset can be loaded based on a specific ID(int) that is sent to the function.
- Unload/Destroy the loaded asset when the user is not using the asset viewer.
- Repeat.
So far, I have written a function that will download the Asset Bundle and Load one/multiple assets at once and destroy previously loaded assets using tags and Destroy(). However, I don’t feel this is the best way of approaching Asset Bundles(because it doesn’t work).
Here is the Function:
function loadAsset(assetID : int){
Debug.Log(loadCount + " assets Loaded");
var assetName : String = "asset_" + assetGroup + "_0" + assetID;
if(loadCount==0){
var assetBundle_link:WWW = new WWW (externalLocation + "AssetBundles/assets.unity3d");
yield assetBundle_link;
Debug.Log("Asset Bundle downloaded from server.");
}
loadCount++;
Debug.Log(loadCount);
if(loadCount>1){
var assetTemp = GameObject.FindWithTag("cleanup");
Destroy (assetTemp);
}
var bundle : AssetBundle = assetBundle_link.assetBundle;
var asset : GameObject;
asset = Instantiate(bundle.Load(assetName));
asset.tag = "cleanup";
}
As I said, this works once but stumbles a second time with a NullReferenceException. I have set it up so it only downloads the Asset Bundle once, if I remove this logic I get an error complaining that an Asset bundle is being created but already exists. Do I need to add a for loop and create a new variable for each asset that is in the bundle? I assumed I could load/unload from an asset bundle into the same variable which is why I have avoided doing that. Also, I am struggling with where to declare the WWW asset bundle as I can only declare it in the function, which I don’t want because it downloads every time the model viewer is opened and I can’t seem to declare it as a global variable. confusion
Any help regarding this is very much appreciated as I am really struggling to get my head around the concept of Bundles.
Thanks.