AssetBundles, Resources.Load, and "Best Practice" in Unity 2017

I was referred to the Unity documentation, which states that the Best Practice for Resources.Load is to not use it.
Instead, to use AssetBundles.

I went through the documentation several times, on multiple pages (tuts, best practice, api, etc.), and it was quite confusing, but not after I was convinced by Unity that AssetBundles are definitely the way to go. Nowhere can I get a clear or complete answer as to how & when assetbundles and their assets are loaded into memory. Not their manifest files. Not one specific asset. The entire bundle.

I downloaded Unity’s AssetBundleManager & sample scenes, and outside the editor they throw an error about not being able to download from my localhost IP. Same problem as seen here but as far as I can tell, there is no resolution.

This sucks. Especially since I am not using AssetBundles to download anything via WWW. No internet required. I was just converting my project by supplanting Resources.Load with AssetBundles, as was told to me by the Unity documentation. I eventually came to realize, looking through AssetBundleManager’s code, that it is specifically made for all the things I don’t need (downloading asset bundles from an external server).

So hours later I decided to stop with AssetBundleManager & removed it from my project. Ugh. I then read to put my AssetBundles in “StreamingAssets” folder inside of Unity, because that is how I am using them: included with the initial full game download. Creating my own assetbundle manager, I begin to test loading the bundles.

AssetBundle bundle2 = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/AssetBundles/Windows/" + "global_player");

I notice the profiler isn’t displaying anything loaded into memory. So when is the AssetBundle actually loaded into memory? Is this just loading the manifests?

Did I just waste several days of my life working hard to read through the documentation, (fail to) understand AssetBundles, and then realize Unity’s suggestion to “Not use Resources.Load” is wrong? Should I just be using Resources.Load? This is so incredibly frustrating. Even the documentation seems inconsistent from page to page, and most of it requires the AssetBundleManager which doesn’t work for me outside the editor.

For the love of god, I just wanted to load my Prefabs into memory so my game’s framerate doesn’t hitch when I instantiate. Resources.LoadAll() makes this so easy. Why the hell are AssetBundles so incredibly complex even when they’re all going to be local files? And how is this best practice? It may be nice updating after the fact, but getting it setup is a PITA, and others have told me they don’t even use it for that - they make their own custom patcher. So in what world is this “best” when Resources.Load() seems to work fine & when you need something more you make a custom solution?

I wouldn’t have spent so much precious dev work days if I knew this wasn’t going to be as easy as it sounds.

  • Create the AssetBundle
  • Load the AssetBundle only when I want
  • Instantiate from the AssetBundle

Nothing about this is simple. And I’ve read multiple doc pages, repeatedly.

7 Likes

It honestly seems like the best way to do anything when it comes to Unity is skim through their documentation, and then just browse the API or use intellisense to see what methods you can call & figure it out from there.

I stopped using that god-awful AssetBundleManager & created my own. I ignored everything I learned in the documentation, and just made a few educated guesses based on the API.

If I am not mistaken, converting my code is as easy as this:

From

UnityEngine.Object[] allObjectsBundle1Prefabs = Resources.LoadAll(ObjectPrefabPath);

To

string AssetBundlePath = Application.streamingAssetsPath + "/AssetBundles/Windows/";
AssetBundle theBundle = AssetBundle.LoadFromFile(AssetBundlePath + "objectsbundle1");
GameObject[] allObjectsBundle1Prefabs = theBundle.LoadAllAssets<GameObject>();

I think that is correct, or at least a start. I will try it out & see.

If AssetBundle.LoadFromFile doesn’t actually load anything but the manifest file (what is essentially just the paths to the assets), then is there any way to simply call AssetBundle.LoadAllAssetBundles() so I can just store references to all the AssetBundles?

2 Likes

Yep…That seems to work.

It was strikingly easy to implement this correctly the moment I completely disregarded everything I read in the Unity docs & just made educated guesses based on the API.

This really, really infuriates me. How can my clueless use of intellisense resolve an issue that overcomplicated, repetitive, poorly written documentation couldn’t?

All I wanted to do was change my Resources.LoadAll() to theBundle.LoadAllAssets(); & that works perfectly. But nowhere in the documentation was there anything even close to this level of simplicity. Just constant pushes of the AssetBundleManager & how great it is… even though it doesn’t even work if your bundles are local.

Creating the AssetBundles was the only part in the docs that made any sense & was very easy & fast. Especially since it was intuitive enough to understand in the UnityEditor Inspector anyway. Click Click Done.

I should have done myself a favor & stopped right after just skimming tiny portions of only some parts of the docs, and then just tried to do it all my clueless self.

3 Likes

Honestly? Don’t use Asset Bundles. What a nightmare. Over 19 hours implementing them already & counting.

The worst part? The build times - in editor. While in the editor, you have to build your AssetBundles anytime you ever change any assets. It takes forever. Unity provides a “Simulation Mode” but I’ve yet to see how much time that will take to implement in my own assetbundle manager system. I don’t use the trash AssetBundleManager - I created my own system to handle assetbundles & assets.

If I never came read the Unity “Best Practices” - I would have just continued to use Resources.Load and wouldn’t have spent what will inevitably be around 20 hours implementing AssetBundles. What a PITA.

Best practice my ass.

1 Like

I’m with you. I never understood the push away from the Resources folder.

Especially since they haven’t bothered to showcase why Asset Bundles are better (this? I guess? https://blogs.unity3d.com/2017/04/12/asset-bundles-vs-resources-a-memory-showdown/ )

AND they haven’t bothered to provide a good workflow.

5 Likes

Well at least I am mostly finished. Finally. 20 hours is a big deal though. For me, unfortunately, that is 1-2 full weeks of gamedev.

But at least now I am at the same part of my game I was at be…fore…!!!

Hehe. Actually I did have a problem where I was loading too much into memory at the start of the game & some of the asset bundle work was making a system which loads/unloads assets to memory at the start of each level. A PITA but it should make memory management easier.

Now instead of 4GB, it should remain <1GB. I am hoping AssetVariants work effortlessly too. For anyone below 4k resolution, like 1080p, the memory req will be a fraction of that.

It was the only way I knew to prevent Unity from hitching/lagging when resources were loaded during play / runtime.

Although some of my GUI still needs to be loaded on demand - not sure how I can prevent the player from seeing any lag while it loads artwork in a book’s next page.

3 Likes

Hey, so just to confirm, if you had to choose between Resources.Load, AssetBundles, and AssetDatabase.LoadAssetAtPath(), which one would you use? I’m a bit of a unity beginner, but I still want to try and do what’s best.

For beginners, just stick with Resources.Load(). AssetBundles are a complicated mess really, with questionable worth.

Stay away from AssetBundles. Maybe even if you’re an advanced user.

Read my quote

1 Like

There are size limits on the resource folder (4gigs) which is why we have to use Asset bundles for larger games.

1 Like

Of course there is. Not surprising at all with Unity, sigh :face_with_spiral_eyes:

1 Like

Sure, Im stating why peeps may choose Asset Bundles over the Resources folder. It’s easier to bite the bullet and use them from the start because moving over from Resources to Asset Bundles is a pain.

1 Like

Yea, no doubt. A 20+ hr pain for me when I did. Wish I had just done so from the start.

Has this ever got easier? I too find it all so confusing, I wish there was an asset to pretty much automate this madness. I only need it to get around 4 gig limit.

The Editor & General Support forums are pretty crowded. If you want to get info or give feedback about asset bundles and related technology, I suggest you use the dedicated subforum.

Unity’s addressing Asset Bundles being an utter pain by building the Addressables system, which should make things smoother. Info here. Warning; still in beta.

2 Likes

@CarterG81 Also FYI for you and anyone else that came across this post.

It took me ~30 minutes to recreate the “Simulation Mode” for loading assetbundles straight in the editor.

It was based off of this snippet from the AssetBundleManager source code
https://bitbucket.org/Unity-Technologies/assetbundledemo/src/493e9bab409473147ef2c0990917b28d80c86d3d/demo/Assets/AssetBundleManager/AssetBundleManager.cs?at=default&fileviewer=file-view-default

if (SimulateAssetBundleInEditor)
 {
     string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
     if (assetPaths.Length == 0)
     {
          Log(LogType.Error, "There is no asset with name \"" + assetName + "\" in " + assetBundleName);
          return null;
     }
     // @TODO: Now we only get the main object from the first asset. Should consider type also.
     UnityEngine.Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]);
     operation = new AssetBundleLoadAssetOperationSimulation(target);
}

Yea, it was easy to do that but that is kindof the problem. Why are these trivial tasks not already part of the default implementation given by Unity?

Anyway, is this even relevant anymore? Didnt they already replace assetbundles, giving up before even finishing like they always do?

What a frustrating lesson in futility. I really dislike UT, especially whoever wrote that assetbundles were best practice when they arent and shortly afterwards were on the path to possible deprecation or replacement with another system. You just never know with Unity… you can never rely on them for anything.

My advice is just pick a version and stick with it until release. Then it doesnt matter what they decide for the future, you’re frozen in place. There really isnt any need to upgrade unless you’re dying for some new feature, which you really shouldnt be unless you just started your project or are still only messing around.

theyre deprecated??? you sure? and what are we meant to use instead?

No they are definitely not deprecated yet.

The addressable system as it is as of April 25 2019 is still very far away from being usable in production.

The AssetBundle Manager is already deprecated.

It makes sense that addressables is still very far away from being usable in production, but that they’re already planning to replace asset bundles. UT seems to have trouble sticking to just one system, and it really ends up biting developers, especially those who dive right in. Then again ever since the UNET fiasco, I am never surprised and don’t trust anything UT does or says. No idea why I thought I’d take them at their word for “best practices” when I originally thought assetbundles would be a good choice to implement. Of course at the worst, I figure I’d just stick with an older version of Unity since implementing assetbundles was such a frustrating mess I don’t care how much better addressables is.

Really it honestly wouldn’t surprise me if addressables was replaced with another system before it ever even replaces assetbundles, hahahahaha… >_>

3 Likes

Just to avoid misinformation and baseless rumor: AssetBundles won’t be deprecated any time soon, especially because the Addressable system is being built on the top of the AssetBundles. The AssetBundle Browser is available through the Package Manager.