- Home /
Resource.Load() vs direct references(as vars in scripts)
The Resources folder is a special folder which allows you to access assets by file path and name in your scripts, rather than by the usual (and recommended) method of direct references (as variables in scripts, wherein you use drag-and-drop in the Unity Editor).
This is said in here. They say rather than by the usual and (recommended) method, which leads be to the conclusion that most of the people use the 2nd method at least for smaller assets. Okay, I used to use it as well, more comfortable and fast, yet sometimes(recently quite often) when some bug with unity appears(mostly minor bugs) and when it gets fixed, many(sometimes all)of my direct refs vars in scripts are unset, something that can be a big pain to be fixed later.
I'm sorry for not being able to give details for reproducing the bug but I would like to ask you 2 things.
1) Should I use mostly direct references(as vars in scripts) for small assets or should I use Resources.Load() instead?
2) Is this a normal thing for unity to happen(losing the direct references) or most likely I'm doing anything wrong?
Answer by bartm4n · Apr 18, 2015 at 03:15 AM
I would suggest using references, regardless of the size of the asset. That is really the way that things are designed to be used.
Anything included in the Resources folder is included in ALL builds, regardless of whether or not it is actually used. This isn't necessarily a problem, but something to be mindful of.
I've actually never encountered a bug where all of my references get lost. I really don't think that this is so much of a bug with Unity as it is a user error. The only way that I know how to reproduce this is by changing the name of or removing the variable, saving the script and returning to the Editor.
Anything included in the Resources folder is included in ALL builds, regardless of whether or not it is actually used.
This is a pretty much good reason for not putting assets in the Resources folder. Does this mean that it's a good practice for assets to be put in the assets folder directly ins$$anonymous$$d in the Resources one? How can I choose where it's better for them to be put?
The only way that I know how to reproduce this is by changing the name of or removing the variable, saving the script and returning to the Editor.
This isn't my case, yet re$$anonymous$$ded me to ask another question, which I should probably ask in a separate thread. How can the result of changing the name of a variable that holds direct refs be avoided i.e. is there a way to avoid losing the refs? I'm asking because many times I wanted to change some var name to a better one but I didn't because didn't feel like readding the refs. Thanks!
A general rule of thumb is to manipulate the assets through the editor when it comes to importing/organizing them. Importing using the editor will automatically place things into the assets folder.
With regard to the variable na$$anonymous$$g, I am really not aware of a way around this. In practice, it is best to pick a variable na$$anonymous$$g standard and stick with it.
If the issue is that you have lots of the same objects that you are having to reset things on, you might consider implementing them as prefabs. This would let you update the reference on the prefab through the Editor and have it update on all of the "clones" in the scene. The only time that this won't work is if you have manually adjusted references on the clone, in which case those references will ignore the prefab settings to the best of my knowledge.
Also individual sprite textures can't be auto-atlased if you use Resources. For rena$$anonymous$$g variables without losing references, this has been asked/answered already several times, e.g. http://answers.unity3d.com/questions/941666/how-do-you-rename-a-public-variable-and-keep-inspe.html
Importing using the editor will automatically place things into the assets folder.
I'm not sure if you're talking about Assets -> Import New Asset option but if you're, I just tried it and it adds the files exactly into the folder that I'm currently at in the project window i.e. if I'm in the Resources folder it's going to add them there.
Admittedly, I typically import asset bundles so my Import New Asset may just be at the default location.
What I was really trying to get at is that you should try to manage all of the files/folders that represent your assets through the editor. This includes moving, rena$$anonymous$$g, deleting etc.
Unity will auto-detect new assets that you place into the assets folder with your OS file explorer, but can have issues keeping track of the assets and their references when you manipulate existing assets through the OS ins$$anonymous$$d of the editor. This does not include editing models or textures, you will of course need to do that through the appropriate software.
http://docs.unity3d.com/$$anonymous$$anual/ImportingAssets.html
Never move any assets or organize this folder from the Explorer (Windows) or Finder (OS X). Always use the Project View
There is a lot of meta data stored about relationships between asset files within Unity. This data is all dependent on where Unity expects to find these assets. If you move an asset from within the Project View, these relationships are maintained. If you move them outside of Unity, these relationships are broken. You’ll then have to manually re-link lots of dependencies, which is something you probably don’t want to do.
So just remember to only save assets to the Assets folder from other applications, and never rename or move files outside of Unity. Always use Project View. You can safely open files for editing from anywhere, of course.
Your answer
Follow this Question
Related Questions
References vs ResoucesFolder: which one to use? 0 Answers
Goods and bads about using Resources.load 4 Answers
Problem in loading images from resources.load(). 2 Answers
Dynamically Load Resources As Needed 0 Answers