- Home /
Does loading a Resource load that resource's dependencies?
If I load a resource, say a Material using Resources.Load() - does that Material's dependencies, such as its normal map texure, get loaded automatically? Or do I also have to manually do a Resources.Load() on the texture used by that loaded Material. And if so, does the loading order matter?
Also, related (this isn't really a separate question): If I'm using Resources.Load() then all loaded assets need to be duplicated into that Resources folder - right?
I mean I've read the docs on Resources.LoadAssetAtPath() but it says it only works in the Editor. So that means I have to use Resources.Load() and that if I have my project organized so all my Materials are in /Materials/ folder and textures in /Images/ folder then that means if I want to load them at runtime, then I have to make a duplicate of them into the Resources folder too? Is that right? Am I crazy in my thinking or is that a bit goofy since I'd now have to keep the 2 materials and 2 images in sync throughout my project folder?
Thanks for any clarification on Resources and Resource dependencies.
Answer by Waz · Jul 05, 2011 at 06:36 AM
A build (for WebPlayer, EXE, etc.) is composed of all Resources, all Scenes that are listed in the build settings, and all their dependencies.
When you "load" a resources, all you are doing is finding the reference to it given its path. Similar, if Level0 has a Foo in the hierarchy and that Foo has a script variable set to a Gumby prefab and that Gumby has a renderer that uses material Baz and that Baz material uses the Jello texture, all these assets of the project are included in the build.
Resources are just the same, except they (and all their dependencies) are automatically included in the build (actually along with Level 0 by default).
Personally, I would prefer this:
var availableDogSkins : Material[];
...
dog.renderer.material = availableDogSkins[choice];
to this:
dog.material = Resources.Load("DogSkin"+choice) as Material;
As you can see, there is no way Unity can work out that DogSkin1, DogSkin2, etc. materials need to be included, those are "hidden dependencies", and hence you'd have to use resources. Whereas in the former code, it only needs to include those materials which you drag to the availableDogSkins array, and it knows those explicitly from your hierarchy.
And yes, it's goofy and crazy thinking to duplicate assets unnecessarily!
Thank you for your detailed answer. :)
Can you please also confirm that if my scene does not have an object in the hierarchy which refers to say, an alternate t-shirt material, and assu$$anonymous$$g I want to dynamically load new t-shirts in game, then I have to copy all my t-shirt materials from their natural place in the folder structure in the project where I have them, into the Resources folder in order to be able to use them in-game with Resources.Load()? I can't tell from your response if you are saying I am goofy for thinking about duplicating resources, or if Unity is goofy for making you duplicate them. ;)
And can you please clarify what is the point of Resources.LoadAssetAtPath() if you can't load a resource from some specific folder within your project?
I think you're under a misassumption about what "dynamically loading new t-shirts" might mean. Unless you are using AssetBundles, the only objects you can "load" in any sense are those built into your project, as deter$$anonymous$$ed by the dependencies as I have explained them. Listing a prefab (my availableDogSikns
) versus loading a Resource are practically the same thing. If you can clarify what differences you are imagining, we can help.
Specifically LoadAssetAtPath only works in the Editor. It's not for runtime use.
sorry, I'm sure I have Unity ter$$anonymous$$ology wrong. I'm trying to equate this to Flash and the "library" in Flash where anything in the library can be instantiated on the stage at runtime.
So say my Unity Project has 1 scene, with 1 FBX in the game scene Hierachy (on the stage whatever it is called) and that FBX has a Tshirt1 material on it. If at runtime I want to put Tshirt2 material on it and that material is nowhere in the stage/game hierarchy but is in the project, then I use Resources.Load(). And that material needs to be wherever it is normally in the project PLUS being in the Resources folder.
Is my mistake equating the Unity Project panel to the Flash library? $$anonymous$$y understanding is that Flash doesn't include assets in the library into the build if it is not used or referenced in code when it is compiled. I assumed Unity does the same? Or is the Unity Project more of a collection of available files you can use in your project- more akin to a Visual Studio solution or something?
I should read up on asset bundles but I thought they were used for over the internet updating of content or in app purchases? Thanks, I do appreciate your clarification. :)
Certainly don't duplicate assets in multiple places. That I'd definitely wrong and unnecessary. Resources can be used as a library concept, but it is just as correct to put the whole "library" of prefabs into an array of references to the assets, in the hierarchy.
Your answer
Follow this Question
Related Questions
Editor doesn't load (some) dependencies with the scene, but standalone does? O_o 1 Answer
Skinned Mesh Renderer Material 1 Answer
Setting a material texture and saving it 1 Answer
map of earth on material (sphere) is distored - Why? 0 Answers
Draw call increased on duplicated object. But not on others 0 Answers