Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am executing the following method from the Unity Start() method:

private void GetFiles()
    List<string> files = new List<string>();
    EnumerateFiles(baseDir, ref files);
    foreach (string assetBundleName in files)
            assetBundle = AssetBundle.LoadFromFile(assetBundleName);
            prefabs.Add(assetBundleName, assetBundle.GetAllAssetNames().ToList().Where(s => s.ToUpper().EndsWith(".PREFAB")).ToList());
        catch
        if (assetBundle != null) { assetBundle.Unload(true); }

I am trying to enumerate through a bunch of asset bundles and look for prefabs. I have no issue getting the list of asset bundles. However, to determine the prefabs inside I need to load the asset bundle and then use something like GetAllAssetNames() to determine the contents. In my case, I look for the asset name ending in Prefab to determine prefabs. The code works fine as long as none of the asset bundles are corrupt.

However, if a asset bundle is corrupt, the application throws a, seemingly uncatchable, exception and ends execution. I have placed the asset bundle load and the GetAllAssetNames() call in a try/catch structure to try to catch such an error and just continue with the next asset bundle but the try/catch does not seem to be able to catch the error.

Any idea how I can catch such an error to prevent the application from crashing?

The Unity exception seems to trip on the assetBundle = AssetBundle.LoadFromFile(assetBundleName);. As such adding a check to see if assetBundle is null, before using it, will not help. – Lord Ashes Aug 5, 2022 at 14:46 Are you sure it is not the assetBundle.Unload that is causing the Expections. Because according to the documentation LoadFromFile should not throw an Expection, but Unload can if you pass false and then attempt to load any more objects from that bundle the operation will throw an InvalidOperationException. – IndieGameDev Aug 5, 2022 at 19:42 Yes. I am sure. When I run it in the Unity Editor it identifies the line that causes the issue. I think that may actually be the issue. It is not throwing a normal exception that could be caught by the try/catch. It seems to be raising a exception in the console log and then halting the method. The application continue to run but the method that includes the LoadFromFile is ended. The odd thing is that in another application where I use LoadFromFile, I am able to survive loading a corrupt asset bundle, so it must be something related to the project itself. – Lord Ashes Aug 6, 2022 at 2:38

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.