Search code examples
c#unity-game-engineassetbundle

errors loading assetbundle in unity


I'll start with the errors themselves. The first is : Assertion failed on expression: 'm_UncompressedBlocksOffsets[blockInd] <= from + dstPos && m_UncompressedBlocksOffsets[blockInd] + uncompressedBlockSize >= from + dstPos' UnityEngine.AssetBundle:LoadFromFile(String)

Followed by : Failed to decompress data for the AssetBundle 'D:/Unity/projects/PQv0.10/Assets/AssetBundles/spritesbundle'. UnityEngine.AssetBundle:LoadFromFile(String)

I've always just used the resources folder for my projects but my current project has outgrown what that method can accommodate. So I'm trying to offload assets (in this case 5000 or so sprites)into an assetbundle and load them as needed at runtime. I figured I could just get a reference to the assetbundle and then call assetbundle.LoadAsset(assetname), substituting my old Resources.Load calls. Here's my attempt at making the assetbundle reference that keeps erroring:

AssetBundle sprites;
void loadSprites()
{
    sprites = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/spritesbundle");                //          " + bundleName);
    if (!sprites) {
        Debug.Log("failed to load assetbundle");
    }
}

For due diligence, here's also the code I used to create that assetbundle. It simple searches my project for png's and packs them into the assetbundle:

public static void SaveSpecificAssets()
{
    AssetBundleBuild build = new AssetBundleBuild();
    build.assetBundleName = "spritesBundle";

    string[] files = AssetDatabase.GetAllAssetPaths();
    List<string> validFiles = new List<string>();
    foreach (string file in files)
    {
        if (file.EndsWith(".png")) validFiles.Add(file);
    }

    build.assetNames = validFiles.ToArray();

    BuildPipeline.BuildAssetBundles("Assets/AssetBundles", new AssetBundleBuild[1] { build }, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}

The assetbundle builds without errors and appears in the directory where it should be. I have no indication that it's faulty other than the errors I get when trying to load it. The manifest it generates contains appropriate items so I think it's working as intended at least.If I'm missing any relevant information, please point it out and I'll try to provide it.

this is the test coroutine I used in place of loadSprites() for testing if it made a difference:

IEnumerator testLoader()
{
    WWW www = WWW.LoadFromCacheOrDownload("file:///" + Application.dataPath + "/AssetBundles/spritesbundle", 1);
    yield return www;

    sprites = www.assetBundle;
}

Solution

  • Try this, for all the PNG's in your project folder, select them all and add them to an AssetBundle via the Editor, Then just build using

    BuildPipeline.BuildAssetBundles("Assets/AssetBundles",BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    

    Then try to load the bundles and see if it shows an error.

    Also, look into the Manifest to see if all the files you intended are listed