- Home /
Exposed asset variables vs Resources
I wonder when I should use which - when is it better to define many exposed variables like Texture
and assign them in the Inspector and when it would be a better idea to use Resources. Any advice?
Answer by whydoidoit · May 08, 2013 at 05:34 PM
There are different schools of thought on this. My rule is this:
Load it from Resources only if you cannot have an inspector assigned reference. Example: I have a ScriptableObject that contains the version number of my app to match to the asset bundles on the server, I need to load this in script very early in a process and so I use Resources.Load so I don't have to be in a particular scene. I am probably going to try to remove this use of Resources.
Keep it in resources if it must be in your build. I use this for fall back shaders from Asset Bundles and other uses of shaders which are otherwise not included unless they are in a scene.
Here's why: everything in Resources is included in your project, including all of the stuff that you used to use but don't any more. If it's a scene reference and you changed it, no problem, it will be excluded. I just spent ages writing a pseudo resources script to remove the horrendous pile of junk added to my project by a third party supplied element where they'd put everything (including family member pictures which they were presumably using for testing) in Resources.
When you use Asset Bundles you can't load from Resources in them.
So my bottom line is this: don't use Resources if there is any other way of getting what you want. You are asking for trouble down the road.
Just to clarify on that asset bundle thing.
If you use resources only in some scenes and you put those scenes into Asset Bundles you probably want to reduce the bloat caused by their resources. If the resources are in a Resources folder, it's in your build and not in your Asset Bundle.
I have equipment in my game. Each equipment is a prefab in a resource folder. They need to be accessed from all scenes, for inventory, actionbars, offline customizing, actual in-game mechanics. They get loaded into an equipment Class. All the resources get loaded every time. Is there trouble down my road?
So long as you are very careful to put that stuff in there then it's fine. What I do is have a standard scene I LoadLevelAdditive and put my shared components in that (Don't Destroy On Load their holder), but of course the approach you are taking is "ok", I wouldn't do it though because strings are fragile compared to my preferred approach of a script with well defined named variables assigned in the inspector.
$$anonymous$$g. if I want to change the "win" music or a sound effect I just open that scene and assign it, not start hunting for strings or rena$$anonymous$$g stuff.
Answer by frarees · May 08, 2013 at 05:34 PM
Resources.Load
will load Object
s at runtime, and them can be loaded at any time (not precisely at scene load, so you can get faster loading times I presume). If those Object
are referenced via inspector over Object
s residing on a scene, they will be loaded the time the scene is loaded.
E.g. imagine a scenario where you need to choose between two atlas (high and low definition). Is best to load them via Resources.Load
, as if you do via inspector both are going to be loaded.
Hope it helps.
That's a good example of Resources.Load - presu$$anonymous$$g you don't have access to Asset Bundles which I'd use in preference.