Search Unity

Access individual assets from asset bundle once they have been downloaded

Discussion in 'Scripting' started by karan667, Jul 27, 2015.

  1. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    I would like to know how to access individual asset once the asset bundle has been downloaded(It Can even be asset downloaded at previous launch). I would like to access the asset as we can access the asset from resources folder.
    @Adam Buckner Really appreciate any help on this
    Thank you.
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
  3. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    Thanks for the replay @Lysander I am a little new to scripting. so a working script will be really nice.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  5. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    Like i said new to scripting, I have tried the manual already. That's why i posted it in the forum.
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Then post the code you're struggling with and we'll help point you in the right direction. We're not here to write scripts for you - otherwise you won't learn anything.

    There's an entire example script in the link I provided with everything you could need.
     
  7. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    Code (CSharp):
    1.  
    2.  private IEnumerator Start()
    3.   {
    4.   string manifestABPath = path + "PC";
    5.  
    6.   //Loading ManifestFile
    7.   var wwwManifest = new WWW(manifestABPath);
    8.   yield return wwwManifest;
    9.   AssetBundle manifestBundle = wwwManifest.assetBundle;
    10.  
    11.   //Load assetbundle Manifest Obj
    12.   AssetBundleManifest manifest = manifestBundle.LoadAsset("PC") as AssetBundleManifest;
    13.   manifestBundle.Unload(false);
    14.  
    15.   //Get dependent assetBundles
    16.   string[] dependentAssetBundles = manifest.GetAllDependencies(bg);
    17.   for (int i = 0; i < dependentAssetBundles.Length; i++)
    18.   {
    19.   Debug.Log(dependentAssetBundles[i]);
    20.   }
    21.  
    22.   //Load Dependent assetBundles
    23.   AssetBundle[] assetBundles = new AssetBundle[dependentAssetBundles.Length];
    24.  
    25.   for (int i = 0; i < dependentAssetBundles.Length; i++)
    26.   {
    27.   string assetBundlePath = path + dependentAssetBundles[i];
    28.  
    29.   //Get the hash
    30.   Hash128 hash = manifest.GetAssetBundleHash(dependentAssetBundles[i]);
    31.   var www = WWW.LoadFromCacheOrDownload(assetBundlePath, hash);
    32.   yield return www;
    33.   assetBundles[i] = www.assetBundle;
    34.   }
    35.  
    36.   //Load Backgroung assetBundle
    37.   var wwwBG = WWW.LoadFromCacheOrDownload(path + bg, manifest.GetAssetBundleHash(bg));
    38.   yield return wwwBG;
    39.   AssetBundle BgBundle = wwwBG.assetBundle;
    40.  
    41.   //Load Backgroung
    42.   GameObject BGPrefab = (GameObject) BgBundle.LoadAsset("bg");
    43.   GameObject.Instantiate(BGPrefab);
    44.  
    45.   //unload dependency assetBundle
    46.   for (int i = 0; i < dependentAssetBundles.Length; i++)
    47.   {
    48.   assetBundles[i].Unload(false);
    49.   }
    50.  
    51.   //Unload bg assetBundle
    52.   BgBundle.Unload(false);
    53.  
    54.   }
    55.  
    This code does not seem to work, but this is what i have so far. Really appreciate your help.
     
  8. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What part doesn't work exactly?
     
  9. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    //Load Backgroung
    GameObject BGPrefab = (GameObject) BgBundle.LoadAsset("bg");
    GameObject.Instantiate(BGPrefab);

    this part does not work. The downloaded asset doesn't appear in the scene.
     
    Last edited: Jul 27, 2015
  10. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
  11. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Sure- you use it on an AssetBundle, give it the name of the asset inside (and its type IMO), and it returns the asset in question- if it exists. It's not complicated at all.

    My advice is "don't assume that LoadAsset is the part that's failing". Don't assume anything. Go through every part of the process that you've built and check all results for null and default values. Beyond that though, I can't really debug what you've written because it has too many variables outside of the script itself. I don't know what your assetbundle name is, what its path is, what the asset's name is within the bundle, what type of asset it is, etc...

    The entire script you wrote looks wrong to me, but I'm not sure exactly what it is that you're trying to do with it. You're pulling a manifest file out of an AssetBundle with LoadAsset? Are you trying to load an AssetBundle that's bundled within another AssetBundle? What is the type of the object you're trying to load, specifically?

    The example of loading a single asset that you'll find at the bottom of the page at http://docs.unity3d.com/Manual/LoadingAssetBundles.html is quite simple and straightforward. What is it that you need which the example isn't providing for you? You need like 10x more debug.logs in that code you wrote- debug EVERYTHING.
     
    Last edited: Jul 28, 2015
  12. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    The entire script i posted was something i was trying out. Here's what i am trying to achieve, I have a asset bundle which has 20+ gameobjects. I want to download the bundle and then access only one asset at a time. I have figured out how to download the asset bundle.
    But i am not able to figure out is after downloading the bundle how to access and instantiate individual objects from the storage of the mobile device.

    I am Trying to load GameObject with texture from the assetbundle.

    Thank you for taking time for replying.
     
  13. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I've gone through your code line by line and I can't find a single thing wrong with it, except that as I said, I can't verify paths and names. Check BGPrefab for null and log it. Check everything for null and lot it. Find out exactly where the sequence is failing. I can't do that for you as I don't have access to the assets you're using.
     
  14. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    when i run the game the gameobject instantiates but any texture that's attached to it does not show up, the gameobject is in grey color
     
  15. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    "attached" to it? That's not how textures work. If you have the texture in your unity assets already, then you need to go into the mesh on the newly-loaded GameObject and assign the texture. If you don't have it, then you need to import the texture separately (using LoadAsset()) and then assign it to the mesh. The GameObject originally had a REFERENCE to a texture, and that reference no longer exists.
     
  16. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    Thanks a lot, ill check out what i can do and get back to you.
     
  17. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    @Lysander Can you tell me what bundle stands for in the following code
    AssetBundleRequest request = bundle.LoadAssetAsync ("myObject", typeof(GameObject))
     
  18. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    I am able to get the game object to be instantiated now but still facing the issue where the gameobject is pink in color.
    I am uploading the project file also can u have a look and let me know what to do.
     

    Attached Files:

  19. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Bundle stands for the assetBundle that you're trying to load the asset from. I've already wasted over an hour on this problem so I won't be downloading your package, sorry. I told you that you need to load the texture onto the GameObject's mesh- it's the same way you always assign textures via script. Google it.
     
  20. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    No problem. Thank you very much for your help.
     
  21. karan667

    karan667

    Joined:
    Jan 24, 2015
    Posts:
    16
    @Lysander I just want to say thank you very much for your help on what i was trying to achive. I figured out what had to be done. Thank you again