Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by pretender · Jun 09, 2010 at 03:56 PM · assetbundleloadasyncstream

assetBundle - get the list of the assets in the bundle

is it possible to get list of assets in the bundle, for example, i dont want to load them by name (because i dont want to know their names), i want to loop through the bundle and load the ones i want...

here is the code i use currently to get assetBundles i made for my game (but i get the mainAsset, it would be still ok to loop through main asset to get all children, ideally it would be to somehow get the list of assets and then to load them with LoadAsync, so they start appearing as soon as they are downloaded):

function StartDownload(url:String,download : WWW,instanced : Object){

  // INDEXOF METHOD RETURNS THE POSITION IN THE STRING OF THE SEARCH SUBSTRING 
  if (url.IndexOf ("file://") == 0 || url.IndexOf ("http://") == 0) 
     //IF THE FILE:// OR HTTP:// ARE IN THE BEGINNING OF THE URL PROCEED TO DOWNLOAD 
     download = new WWW (url); 
  if (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer) 
     //IF WE ARE EXECUTING IN THE PLAYER 
     download = new WWW (url); 
  else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsEditor) 
     //IF WE ARE EXECUTING LOCALLY 
     download = new WWW ("file://" + Application.dataPath + "/Bundles/" + url); 

  //PLACE HERE THE PROGRESS CODE (progress variable has to be global and private 
  if(download.error==null){ 
     //   progressString=parseInt(download.progress*100).ToString();
        //Debug.Log(download.progress); 
  } else { 
        Debug.Log(download.error); 
  } 

  //progressString=parseInt(download.progress*100).ToString(); 

  //WAIT FOR IT TO FINISH 
  yield download;          

  //ASSIGN ASSETBUNDLE FROM DOWNLOAD TO ASSETBUNDLE OBJECT 
  assetBundle = download.assetBundle; 

  //WE CAN NOW RELEASE DOWNLOAD BY 
  download.Dispose(); 
  download=null; 
  //SO THAT WE CAN USE IT LATER ON THE NEXT OBJECT 

  //IF ASSETBUNDLE ISNT NULL 
  if (assetBundle != null) 
  { 

     // Alternatively you can also load an asset by name (assetBundle.Load("my asset name")) 
     var go : Object = assetBundle.mainAsset; 

     //IF  EVERYTHING IS OK 
     if (go != null) 
        //INSTANTIATE THE ASSET BUNDLE 
        instanced = Instantiate(go); 
     else 
        Debug.Log("Couldnt load resource");    
  } 
  else 
  { 
     Debug.Log("Couldnt load resource");    
  } 

}

any help or direction is appreciated! thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by edwood_grant · Jul 28, 2010 at 11:29 PM

As far as I know, no you can't list all the assets inside and assetbundle.

However, you can use

Object.FindObjectsOfType (type : Type) : Object[]
Object.FindObjectOfType (type : Type) : Object

After getting your mainasset bundle to load objects by type.

You could also use

AssetBundle.LoadAll(type: Type)

To load all the items by type

Or you can use

AssetBundle.Contains(name : string) : bool

To find if any asset you are looking for by name exists (although, by the nature of your question, you might not want that)

Hope this helps you, Italo F. Capasso B. AKA "Edwood Grant"

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pretender · Jul 29, 2010 at 12:49 PM 0
Share

the problem was that i wanted to get just one object from the bundle, but you cant do it that way. you have to download all of them and then i can just display one if i want. so i decided not to go with them.

avatar image
2

Answer by riffraff · Jun 14, 2012 at 02:16 PM

There is simple technique used in "CharacterCustomization" example: http://unity3d.com/support/resources/example-projects/charactercustomization

in short:

During creation of assetbundle you may create list of objects names, then add it to instance of StringHolder:

public class StringHolder : ScriptableObject { public string[] content; }

StringHolder holder = ScriptableObject.CreateInstance (); holder.content = listOfNames.ToArray(); AssetDatabase.CreateAsset(holder, "Assets/objects_names.asset");

and then add this asset to assetbundle as you did with "real" objects.

At loading time just: StringHolder names = WWW.assetBundle.Load("objects_names", typeof(StringHolder));

so you will know what is inside loaded asset.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flamy · Jul 17, 2012 at 04:41 AM 0
Share

great idea, i was breaking my head with this for some time :)

avatar image
0

Answer by riffraff · Jun 14, 2012 at 04:36 PM

You may use simple technique from http://unity3d.com/support/resources/example-projects/charactercustomization

During creation of asset write also list of names, like this:

 public class StringHolder : ScriptableObject
 {
     public string[] content;
 }

 StringHolder holder = ScriptableObject.CreateInstance<StringHolder>();
 holder.content = listOfNames.ToArray();
 AssetDatabase.CreateAsset(holder, "Assets/objects_names.asset");

and export this asset with other.


at reading just:

 StringHolder objects_names = WWW.assetBundle.LoadAsync("objects_names", typeof(StringHolder));

and that's all.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

Answer by kefrens · Dec 08, 2012 at 04:19 PM

Update: Lately I've encountered problem with loading StringHolder from WebPlayer. Some "the class defined in script file named 'StringHolder' does not match the file name!" garbage. So i've come to another solution of creating transitory TextAsset with names of objects in bundle:

 // first temporary add txt file to asset database
 string assets_names; // contains all names
 string path_to_names_in_data = "Resources\\objects_names.txt";
 File.WriteAllText( Path.Combine( Application.dataPath, path_to_names_in_data ), assets_names );
 
 // import txt as TextAsset
 AssetDatabase.ImportAsset( "Assets/Resources/objects_names.txt", ImportAssetOptions.ForceSynchronousImport );
 
 // get this as TextAsset object
 TextAsset text_asset = ( TextAsset )AssetDatabase.LoadAssetAtPath( "Assets/Resources/objects_names.txt", typeof( TextAsset ) );
 
 // add it to export array
 Object[] export_selection = new Object[1 + current_selection.Length];
 export_selection[ 0 ] = text_asset;
 
 // build bundle:
 BuildPipeline.BuildAssetBundle(Selection.activeObject, export_selection, path, BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets );
 
 // release TextAsset
 AssetDatabase.DeleteAsset( AssetDatabase.GetAssetPath( text_asset ) );
 UnityEngine.Object.DestroyImmediate( text_asset );


Now I can load this TextAsset at runtime like following:

     TextAsset objects_in_bundle = ( TextAsset )m_asset_bundle.Load( "objects_names", typeof( TextAsset ) );
     string assets_names = objects_in_bundle.text;
 

Above looks complicated, but has no runtime issues. Hope that may help. :-)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by neilsarkar · Dec 10, 2020 at 06:17 AM

This old thread still shows up high in google search rankings so it's worth mentioning that now you definitely can get a list of assets by name.

 var names = assetBundle.GetAllAssetNames();
 
 foreach(var name in names) {
   Debug.Log($"name={name}");
 }

https://docs.unity3d.com/ScriptReference/AssetBundle.GetAllAssetNames.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

When AssetBundle.LoadAsync is necessary ? 0 Answers

SceneManager.LoadSceneAsync freeze loading scene in editor. 0 Answers

Loading Asset Bundle from raw bytes 1 Answer

How to import the object from server to unity 2 Answers

AssetBundle - How to load older version unity asset bundle in latest version asset bundle 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges