- Home /
Is there a unified method for loading assets?
We need the app size to be below 50MB when the user downloads it for the first time (mobile app, from the App Store), and then we load the rest of the resources from our private server.
My understanding is that this will put our resources into two different locations, and therefore the loading mechanism is a little different for each.
As I understand Unity3D, at this stage of my studies, there are two primary ways to load assets in Unity:
WWW.LoadFromCacheOrDownload()
AssetBundle.Load()
My understanding is that I would use AssetBundle.Load() for those assets that are included in the application binary package. If there is an asset bundle, perhaps with a new game level and creatures, then I could grab that (say from a server) via LoadFromCacheOrDownload().
Subsequently, resources can become split into two piles:
Assets > Resources folder
the local cache
The result of this seems to be that the loading mechanism is dependent upon where the resource lives physically on the disk. This strikes me as a little bit strange: a resource is a resource, just load the darn thing.
As such, it seems to me that every time I load a resource, perhaps as dictated by a server-side XML file with a level construction recipe, I have to check every resource for existence in both places. It might be a static file, it might have been dynamically downloaded and cached, and it may not be known at compile time what combination of things will be combined and displayed to the player.
I want to be able to build scripts that don't have to worry about this level of detail. I just want to load the resource if it is exists, no matter where it exists, and use it.
Is my understanding of this situation accurate? If so, is there a standard Unity call that achieves what I'm looking for? Perhaps my thinking about the entire matter is topsy-turvy. If so, please feel free to call me out on it or question my assumptions.
Thank you.
Answer by dorpeleg · Oct 08, 2013 at 12:34 PM
You got it a bit wrong.
WWW.LoadFromCacheOrDownload() and AssetBundle.Load() are used together.
LoadFromCacheOrDownload works like so:
Check to see if the requested file (by URL and version) is already cached (saved locally).
If it is, load it from there (AssetBundle = download).
If it is not, download it from the URL (AssetBundle = download when download completes).
After the LoadFromCacheOrDownload is done (either from cache or from URL) you use AssetBundle.Load() to load object from the bundle to where you want.
For example:
GameObject go = AssetBundle.Load("something");
I hope I explained everything you wanted.
I see. Does this mean that in the case of multiple asset bundles, some of which were embedded in the initial app and some of which were downloaded and cached, that I have to always check both the Resources and the Cache every time, to find a requested resource?
Asset bundle A is embedded in the app and has a prefab and sound effect. Asset bundle B is downloaded and contains a new prefab (say, a new sword).
How do you handle game object instantiations of prefabs in a consistent manner when the resources themselves are now scattered? Calling objects probably don't know, nor even care, about where a resource lives.
If you have stuff embedded into the app, you don't need to use asset bundles and LoadFromCacheOrDownload.
For stuff that are embedded (included in build) use: Resources.Load().
$$anonymous$$eep in $$anonymous$$d that in order to use Resources.Load you have to put your assets in a "Resources" folder in the project.
How do you handle game object instantiations of prefabs in a consistent manner when the resources themselves are now scattered? Calling objects probably don't know, nor even care, about where a resource lives.
You should think about it more.
Do you really need assets to be on a remote server?
Do you really need assets to be included in the app?
Are there same type of assets on both server and app?
In other words, think about the best way to keep things organised.
I might be able to give you more tips of you can share more info.
Thank you for your continuing help. To answer your questions directly:
Yes, I do need assets to be on a remote server. It is an absolute requirement for the project.
Yes, I do need initial assets for the game tutorial to be included in the app at first download. This is also a requirement from management.
When you say the "same type", do you mean "type" as in "textures"? If so, then yes, some textures might come in a future asset bundle download from the server, as new enemies are released for special events. So, imagine a level where the player fights monsters. The basic monsters in a newly-downloaded level remain the same as always, just basic grunts to kill, and the boss is a special monster that came with the new asset bundle.
Ok, so you should try and write your code is such a way that it will know when it needs to download from web and when it doesn't.
I don't think it will be very complicated.
So in your example, you load all basic grunts using Resources.Load and load the boss using LoadFromCacheOrDownload.
If the level and the boss always go together, you can put them both in the same asset bundle.
That basically gets at the heart of the matter. So in that example the grunts have one loading mechanism and the boss has another. This means that before loading, we need to know where the resource is.
Let us assume that a generic script is responsible for assembling the level. That script doesn't necessarily know which resource comes from which asset, especially for future asset bundle releases.
It sounds like some kind of Resource $$anonymous$$anager needs to keep track of which resource lives where? Then, the generic level building script would not call Resources.Load directly, but rather a hypothetical "Resource$$anonymous$$anager.Load" function. The $$anonymous$$anager would decide whether to use Resources.Load or LoadFromCacheOrDownload, depending on the situation.
Do I have this straight? Does Unity already have such a function?
Your answer
Follow this Question
Related Questions
I want to build in the resources folder, except for 0 Answers
Find all assetbundles in folder 0 Answers