Hello community! I’ve seen many questions discussing the difference between Serialized private members and public ones. I couldn’t find an answer to a similar but different problem though, hence my question:
I need to access a Material from within my script. As far as I know I can do one of two things:
- Create a variable of type Material with [SerializeField] modifier and drag the desired item in the inspector to connect it.
- Use Resources.Load() and assign it to a variable of type Material
My question is if there is any reason to use one over the other, performance-wise or other?
I should add that the script will be tied to a prefab, which will be instantiated quite a few times, so the script will also get executed many times.
Best wishes and thank you in advance
###2.1. Best Practices for the Resources System
Don’t use it.
This strong recommendation is made for several reasons:
-
Use of the Resources folder makes fine-grained memory management more difficult.
-
Improper use of Resources folders will increase application startup time and the length of builds.
-
As the number of Resources folders increases, management of the Assets within those folders becomes very difficult.
-
The Resources system degrades a project’s ability to deliver custom content to specific platforms and eliminates the possibility of incremental content upgrades.
-
AssetBundle Variants are Unity’s primary tool for adjusting content on a per-device basis.
Source : Assets, Resources and AssetBundles - Unity Learn
Moreover :
- If you change the name / location of the asset, you will have to find very references of this asset in your code and change the path, and, since the path is a simple string, it’s very hard to track it all over you code.
- The assets you put here may be accessible by the users after the build (at least, on a PC build)
- The assets put in the Resources folder are directly put in the build, without compression or optimization from Unity, and are not removed even if you never use them in your code (Unity removes the unused assets in the non-Resource folders.)
Also, check this post : Goods and bads about using Resources.load - Questions & Answers - Unity Discussions