3

I want to load and instantiate asset from my asset bundle, but in unity 5.+ i can do it with code like this: Note: my assetbundle have one asset inside itself, like:

AssetBundle myLoadedAssetBundle;
public string path;

void Start()
{
    LoadAssetBundle(path);
}

void LoadAssetBundle(string bundleUrl)
{
    myLoadedAssetBundle = AssetBundle.LoadFromFile(bundleUrl);
    Debug.Log(myLoadedAssetBundle == null ? "Faild To Load" : "Succesfully Loaded!");
    Debug.Log(myLoadedAssetBundle.mainAsset.name);

    Instantiate(myLoadedAssetBundle.mainAsset);

}

}

actually even i use

Debug.Log(myLoadedAssetBundle.mainAsset.name);

to log name of mainasset of my assetbundle!

but in unity after 5+ , they say mainasset is obsolete.

my questions is:

1- How can i load asset bundle which don't know the name of asset?

2- How Instantiate or assign sprite which loaded of assetbundle ?

Rooz
  • 215
  • 3
  • 13

3 Answers3

4

According to the AssetBundle documentation, there is now the ability to call GetAllAssetNames() to return a list of all asset names in a bundle as a string array. You can load the assets from the bundle via a for loop with those asset names.

Alternatively, if you are guaranteeing to only have 1 asset per bundle you can simply grab the string at index 0 (not recommended).

Additionally, you can load the assets without knowing the names by using LoadAllAssets() which returns an array of the specific type (in your case you would specify Sprite).

Erik Overflow
  • 2,220
  • 1
  • 5
  • 16
  • thanks man, but in the past i try :myLoadedAssetBundle.LoadAsset(myLoadedAssetBundle.GetAllAssetNames()[0]) but , didn't work! – Rooz Dec 05 '19 at 07:55
  • Have you confirmed that GetAllAssetNames()[0] containsa a non-null value? Consider using LoadAllAssets() and checking to see that you receive back an array of gameObjects. @Rooz – Erik Overflow Dec 05 '19 at 15:13
0

You could try something like this: (Note - You will need to know your directory path to each asset, here, I'll make some stuff up as an example)

private List<GameObject> assetBundle = new List<GameObject>();
private GameObject asset1 = Resources.Load("Weapon1") as GameObject;
private GameObject asset2 = Resources.Load("Weapon2") as GameObject;  
private GameObject asset3 = Resources.Load("Weapon3") as GameObject; 

private void Start()
{
assetBundle.Add(asset1);
assetBundle.Add(asset2);
assetBundle.Add(asset3);
}

Now you can access them from the list by element whenever you wish. However, I'm not 100% certain that Resources.Load() will work for what you're trying to do. I am about 65% - 70% though so I answered. Hope it helps!

AntonioTorro
  • 199
  • 4
  • 20
0

Here's my solution,

IEnumerator LoadBundleFromDir(string bundleUrl)
{
    //which type of asset i want load
    GameObject model;
    AssetBundleCreateRequest bundleRequest = AssetBundle.LoadFromFileAsync(bundleUrl);
    yield return bundleRequest;
    AssetBundle localeAssetBundle = bundleRequest.assetBundle;

    Object[] allBundles = localeAssetBundle.LoadAllAssets();
    foreach (Object item in allBundles)
    {
        if (item.GetType() == typeof(GameObject))
        {
            AssetBundleRequest assetRequest = localeAssetBundle.LoadAssetAsync<GameObject>(item.name);
            yield return assetRequest;
            model = assetRequest.asset as GameObject;
            localeAssetBundle.Unload(false);
            break;
        }
    }
}
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
DarkTengo
  • 1
  • 1