Search code examples
androidunity-game-engineassetsassetbundle

How I can test AssetBundle (DLC) locally on Android in Unity?


I'm trying to test some AssetBundles for the game that I'm currently working on. I want to use this because I don't want the game to take a lot of space. I still don't know how servers work and how to upload them over there. I was searching on how to do this and found some neat stuff over here: AssetBundle (DLC) data load to Android phone at runtime [UNITY] But it tells something about uploading to a server and stuff, but I want to test it locally. Any recommendation?

Following what Remy told me, my code looks like this:

public class LoadFromFileExample : MonoBehaviour {

// Use this for initialization
void Start () {

    string fileName = "planes";
    var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, fileName));
    if (myLoadedAssetBundle == null)
    {
        Debug.Log("Failed to load AssetBundle!");
        return;
    }
    var prefab = myLoadedAssetBundle.LoadAsset< GameObject > ("andy");
    Instantiate(prefab);

    myLoadedAssetBundle.Unload(false);

}

// Update is called once per frame
void Update () {

}
} 

But it shows the following error: Unable to open archive file: C:/Users/Chris/Desktop/myDLC/Assets/StreamingAssets/myassetBundle

This is the asset bundle name


Solution

  • Take a look at AssetBundle.LoadFromFile.

    This allows you to load in your assetbundle file from the local storage of the device. Meaning you don't have to upload/download them first.

    it will look something like the following:

    string fileName = "fooAssetBundle";//name of the assetbundle you want to load
    
    var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, fileName));//Creates a filepath starting at the streamingAssetsPath and appends filename to it. 
    
    var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("MyObject");//Create a GameObject from the assetbundle
    Instantiate(prefab);//instantiate the GameObject
    
    myLoadedAssetBundle.Unload(false);//Unload the assetbundle from memory as it isn't used anymore
    

    The above example uses the Application.StreamingAssetsPath but this can be any path your desire like Application.PersistentDataPath or an external storage.