6

I am using unity5.3.3, I would like to know how should I get the asset from an asset bundle, whose names are same but are kept in different folder. My AssetBundle Folder is set in the following manner:

 MyAssets -> this Folder is packed as an AssetBundle
  -ThemeOne(folder)
     - Logo.png
  -ThemerTwo(folder)
     - Logo.Png

When I do AssetBundle.LoadAssetAsync("Logo"). I end getting the logo in the first(ThemeOne) folder. So how do I access the file in the other folder?

I have just created a sample project so that you can check it out. Check the Folder Assets\AssetBundleSample\SampleAssets\Theme and the script LoadAssets

Sushant Poojary
  • 353
  • 4
  • 12

5 Answers5

0

You can put the ThemeOne(folder) and ThemeTwo(folder) in Resources Folder under assets and than use something like this

(AudioClip)(Resources.Load ("Sounds/" + "myaudioclip", typeof(AudioClip)) as AudioClip)

similarly load your png giving folder name first as i have given sounds and than name of file as i have given myaudioclip . cast as texture or something as you want

Sourav Sachdeva
  • 353
  • 6
  • 20
  • I don't want to embed these assets with the game initially by putting them in Resource folder. Rather want to download the assetBundle at later point in time and then load the individual assets. – Sushant Poojary May 10 '16 at 06:14
  • Have you created the asset Bundle successfully because i have read somewhere that it won't let you add assets with same name even in different folders. – Sourav Sachdeva May 10 '16 at 06:36
  • Yes in the older AssetBundle API you couldn't create the assetBundle unless you used BuildPipeline.BuildAssetBundleExplicitAssetNames. But I have created the assetbundle successfully in unity5. So am wondering if am missing something trivial here. – Sushant Poojary May 10 '16 at 06:54
  • doesn't LoadAssetAsync works like this.LoadAssetAsync (Assets/ThemeOne/Logo.png) – Sourav Sachdeva May 10 '16 at 09:13
0

As unity official docs provided

public string BundleURL;
   public string AssetName;
   IEnumerator Start() {
     // Download the file from the URL. It will not be saved in the Cache
     using (WWW www = new WWW(BundleURL)) {
         yield return www;
         if (www.error != null)
             throw new Exception("WWW download had an error:" + www.error);
         AssetBundle bundle = www.assetBundle;
         if (AssetName == "")
             Instantiate(bundle.mainAsset);
         else
             Instantiate(bundle.LoadAsset(AssetName));
                   // Unload the AssetBundles compressed contents to conserve memory
                   bundle.Unload(false);

     } // memory is freed from the web stream (www.Dispose() gets called implicitly)
   }

BundleURL will use the path where your actual assets is located. You can also use Application.dataPath in conjunction with ur specified path string.

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • 2
    I have actually loaded the assetbundle. Where I am stuck is at this point `bundle.LoadAsset(AssetName)` . When I do `bundle.LoadAsset("Logo")` I only the asset from the first folder. – Sushant Poojary May 10 '16 at 07:36
  • as i said you need to change URL for accessing the second logo. you have to change the path according to your location. simple – Muhammad Faizan Khan May 10 '16 at 07:42
  • 1
    Both assets are in one single AssetBundle. The problem is not with loading of assetBundle, it is accessing assets inside the assetbundle once it is loaded in the memory. – Sushant Poojary May 10 '16 at 07:50
  • so there is only single file name which you will use if it contains both assets then surely it will load it – Muhammad Faizan Khan May 10 '16 at 07:58
0

Seems to be a bug. Still present in Unity version 5.3.3.

See: http://answers.unity3d.com/questions/1083400/asset-bundle-cant-have-multiple-files-with-the-sam.html

Matti Jokipii
  • 561
  • 1
  • 6
  • 20
0

I'm not really sure if this is possible, you should read Explicit naming of assets which is FYI obsolete as on Unity 5.6. This allows you to explicitly name assets that you are including in the bundle. Alternatively, you could try,

bundle.LoadAsset("/Assets/ThemerTwo/Logo.png")

But generally, this is a bad practice to have assets of the same naming in the same bundle.

Arvind
  • 730
  • 10
  • 20
-1

You can use the method LoadAssetAtPath() to specify the full path instead of just the asset name.

Example:

LoadAssetAtPath("Assets/Texture/Logo.jpg", typeof(Texture2D));

Documentation here: http://docs.unity3d.com/ScriptReference/AssetDatabase.LoadAssetAtPath.html

Stuart Thompson
  • 1,839
  • 2
  • 15
  • 17
  • The folder is packed as an assetBundle and am using the Assetbundle API to load assets. So I load the assetBundle at runtime and load individual assets from it. So LoadAssetAtPath is not the solution. – Sushant Poojary May 10 '16 at 06:11