- Home /
Resources.Load without a Resources folder?
Is there any way to use Resources.Load with a file path from the root assets directory rather than a folder called Resources? Should I just put everything in a folder called Resources? I mean I could do just that, but would that cause any problems if I put everything except the Editor and Standard Assets folder in one big Resources folder?
It is just good housekeeping to have anything that may be required in the build that may be loaded at runtime to be in a Resources folder.
http://docs.unity3d.com/$$anonymous$$anual/LoadingResourcesatRuntime.html : "Resource Folders are collections of assets that are included in the built Unity player, but are not necessarily linked to any GameObject in the Inspector."
Personally, all assets should be in a folder, any folder; not just random objects/textures/scripts dumped in the root assets folder.
"Should I just put everything in a folder called Resources?" : no. Everything in a resources folder will be included in the build, even if you never use it (attached to a gameobject in the inspector or possibly required at runtime). Again, it all comes down to how you manage your project window.
The answer to your question is Application.dataPath. This points to the assets folder in the editor, and the _Data folder in the build : http://docs.unity3d.com/ScriptReference/Application-dataPath.html
But you are going to have a really bad time if you do that, having to copy and paste those things into the data folder of your build. That's if it will even work, because you will then have to use WWW, and some asset types will just not be able to be loaded : http://docs.unity3d.com/ScriptReference/WWW.html
In short, organize your project window. Put everything in a folder (named scripts/textures/materials/models/prefabs/sound/whatever). Anything that is not attached in a scene but may be required to be loaded at runtime, put in a Resources folder.
$$anonymous$$ore experienced users will be able to explain how something that is instantiated doesn't necessarily need to be in a Resources folder, and how Resources.Load helps with the memory footprint of a build when running. Personally the only thing I put in a resources folder is something that will be network instantiated.
Let's see if I can explain it better...
If you have something that is not in a scene, but will definitely be used in the game, then store a reference to it in a script, then call on that reference (can be in any folder):
public GameObject myBarrel;
// ...
Instantiate( myBarrel, pos, rot );
If you have something that only might be required, then use Resources.Load, and it needs to be in a Resources folder:
Texture superEpicPlayerSkinDLC = Resources.Load( "SuperEpicPlayerSkinDLC" ) as Texture;
Answer by Immanuel-Scholz · Apr 14, 2016 at 12:05 PM
During the player, there is no function to read assets directly from outside a Resources/ folder. Its not even possible, because if the asset is never linked and is not inside a Resources/ folder, it is completely removed from the build.
Within the Editor, just use AssetDatabase.LoadAssetAtPath()
You can move as much as you like into a Resources/ folder. You can also have subfolders within Resources/ folder and you can have a Resources/ folder within a subfolder.
Example:
Assets/Resources/basics.bytes
Assets/art/Resources/Sprites/mysprite.tga
Assets/code/Resources/sample.prefab
You can load these objects like this:
byte[] basics = Resources.Load<TextAsset>("basics").bytes;
Sprite mysprite = Resources.Load<Sprite>("Sprites/mysprite");
GameObject samplePrefab = Resources.Load<GameObject>("sample");
Two things to remember here:
You need to specify the sub-folder when loading via
Resources.Load
Everything that is inside a Resources folder will be added to the final build, so be sure to remove all test assets and garbage before shipping to reduce game build size. (That is also true for all "test-GameObject's" that you leave in any scene that is shipped. Even if the GameObject is deactivated, all assets it references will be included in the build).
There is another option of dynamic resource loading: AssetBundles. Basically, you can pack any asset into an AssetBundle (from any folder) and later in the game load that bundle and then load things out of the AssetBundle in a similar syntax to Resources.Load
.
Your answer
Follow this Question
Related Questions
Won't read files from directory on android - Windows works fine. 0 Answers
Store the path with Resources.Load in a string 3 Answers
Resources.Load returns null,Resources.Load don't load sprite 1 Answer
How to deal with discrepancy in application file path between running the exe vs running from Unity? 0 Answers