Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

bundle.LoadAssetWithSubAssetsAsync(bundle.GetAllAssetNames()[n]) => null

Discussion in 'Unity 5 Pre-order Beta' started by cory_munky, Feb 5, 2015.

  1. cory_munky

    cory_munky

    Joined:
    Dec 10, 2014
    Posts:
    66
    Maybe I'm misunderstanding how this is supposed to work, but I'm confused by an asset that the bundle claims to have but fails to load. The good news is that the failure case is pretty specific. Maybe someone can tell me what I am misunderstanding. (Beta 21, btw)

    The failing code is:
    Code (csharp):
    1. foreach (string name in bundle.GetAllAssetNames())
    2. {
    3.     if (name.EndsWith(".prefab"))
    4.     { // Load each prefab
    5.         AssetBundleRequest request = bundle.LoadAssetWithSubAssetsAsync(name);
    6.         yield return request;
    7.         if (request.asset == null && request.allAssets.Length == 0)
    8.             Debug.LogErrorFormat("Bundled asset failed to load : {0} from {1}", name, url);
    The bundle was packed using:
    Code (csharp):
    1. string[] prefabPaths = Directory.GetFiles(baseDir, "*.prefab", SearchOption.AllDirectories);
    2. Object[] loadedAssets = prefabPaths.Select<string, Object>(assetPath => AssetDatabase.LoadMainAssetAtPath(assetPath)).ToArray();
    3. BuildAssetBundleOptions options = BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.AppendHashToAssetBundleName | BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CollectDependencies;
    4. BuildPipeline.BuildAssetBundle(null, loadedAssets, outputPath, out crc, options, platform);

    The failure case is a prefab that is not located under baseDir. There is a character prefab under baseDir with a script component that references a weapon prefab that is not under baseDir. The weapon prefab appears in the list of names returned by GetAllAssetNames(). But, attempting to load that asset name from the bundle fails. Switching the bundle packing to use LoadAllAssetsAtPath or LoadAllAssetRepresentationsAtPath gives the same results.

    I would expect CollectDependencies to package the weapon prefab in the bundle. I would not expect bundle.GetAllAssetNames() to return the names of assets that are not in the bundle. Am I mistaken? Thanks!
     
    OrenMunkyFun likes this.
  2. cory_munky

    cory_munky

    Joined:
    Dec 10, 2014
    Posts:
    66
    I got excited with Beta22's release notes saying
    • AssetBundle: Only write the explicitly included assets to the manifest file.
    But, I just tested with B22 and the same thing happens.

    To be more clear than my first post: I have parent.prefab which references child.prefab. If I BuildAssetBundle(only parent.prefab) then that bundle's GetAllAssetNames() lists both parent and child. But, if I attempt to load child.prefab from that bundle, I get null.

    Is this expected behavior or is it a bug?
     
    OrenMunkyFun likes this.