The UI, .meta files, AssetImporter and the AssetDatabase are all disagreeing about our Bundle tags

Summary: Apparently, the assets in our cache server have asset bundle tags and this is confusing Unity when these cached tags conflict with the tags in the .meta files. If I reimport assets or delete Library and restart without connecting to out cache server, the problem does not appear.

So, I was in a situation where calling AssetImporter.GetAtPath(path).assetBundleName would give me a non-empty name, but AssetDatabase.GetAssetPathsFromAssetBundle(importer.assetBundleName).Length == 0

This meant that even though the UI did not show any assets as tags with bundles, and none of the .meta files contained any mention of bundles, the importer.assetBundleName would still claim the asset was assigned to a bundle. This is after restarting with an emptied Library folder.

Fortunately, the bundle names were consistent with what I wanted them to be. I think they were cached from an earlier experiment in auto-assigning bundle names. So, I ran my auto-assign script again, and…

Now the .meta files say they are tagged with bundles, the AssetImporters think they are tagged, but the UI and GetAssetPathsFromAssetBundle still think that they are not!

:frowning:

They say there are only two hard problems in computer science. I’m just trying to solve the problem of naming things. But, I need Unity to solve the problem of cache invalidation for themselves.

I don’t think I can upload a repro project for this one. Not unless there’s some way to upload the state of the cache server…

Which version of Unity are you using? There is bug at Cache server with AssetBundle tag. We fixed in 5.2, also back ported to 5.0 and 5.1 patch releases.

We are no longer seeing this issue. I am using 5.1.5p3

Thanks!

Actually, I take that back. I am seeing a variation of the bug again. I am using 5.1.2f1. A big difference is that previously, the AssetBundle tag in the editor would display as blank instead of “None” or a bundle name. Now the UI displays the tag correctly, the .meta file contains the tag correctly, but the assets are sometimes not included in the bundles anyway. AssetDatabase.GetAssetPathsFromAssetBundle() does not report all of the assets that are tagged for that bundle and the manifest for the bundle shows that some assets are not included.

The sequence of events that led to this for me was:

  • An artist created an asset without assigning a bundle tag. It was uploaded to the cache server and to source control.
  • I synched the project from source control and thereby synched the asset from the cache server
  • I ran a script to assign a bundle tag to the asset via the AssetImporter interface

Now the UI and the .meta files show the assets as tags with bundles, but after rebuilding the bundles, those bundles still do not include those new assets.

If I locally reimport the new asset, then the asset does start to be included in it’s bundle. Deleting Library and resynching from the cashe server does not fix the problem.

For reference, this test function finds many errors when I run it on my machine:

    public static void CheckAssetBundleAssignments()
    {
        var assetsFromBundleName = AssetDatabase.GetAllAssetBundleNames()
                                                .ToDictionary(
                                                    name => name.ToLower(),
                                                    name => new HashSet<string>(
                                                                AssetDatabase.GetAssetPathsFromAssetBundle(name)
                                                                             .Select(path => path.ToLower())));
        string[] paths = AssetDatabase.FindAssets("t:prefab")
                                      .Select(guid => AssetDatabase.GUIDToAssetPath(guid).ToLower())
                                      .ToArray();
        int errors = 0;
        foreach (string assetPath in paths)
        {
            AssetImporter importer = AssetImporter.GetAtPath(assetPath);
            if (importer && importer.assetBundleName != null && importer.assetBundleName != "")
            {
                HashSet<string> assets;
                if (assetsFromBundleName.TryGetValue(importer.assetBundleName, out assets))
                {
                    if (!assets.Contains(assetPath))
                    {
                        errors++;
                    }
                }
            }
        }
    }

Interestingly, reimporting through the editor UI fixes the issue, but reimporting through code does not…
Even this code does not affect the issue:

                    if (!assets.Contains(assetPath))
                    {
                        errors++;
                        string bundleName = importer.assetBundleName;
                        importer.assetBundleName = "";
                        importer.SaveAndReimport();
                        importer.assetBundleName = bundleName;
                        importer.SaveAndReimport();
                    }

Also, the problem is definitely the cache server. If I disconnect from the cache server, close the editor, delete Library, reopen the editor, then there are no errors after the long import process.