Although this question is fairly old, it popped up in the feed recently and I thought it might be worth un updated answer.
Basically, does it make more sense to create a singleton script that holds all my prefabs inside it so I can easily access them and instantiate through the game?
If this singleton gets loaded into memory, it will also load all its dependencies, meaning you have all the prefabs in memory at the same time, even if you're using only a few of them.
If your game has the headroom to do this, then it's a convenient way to go. But if you're trying to conserve memory, then this should be avoided.
Or does it make more sense to keep all my assets inside the Resources folder and then call Resources.Load
throughout the game
In modern Unity versions, you can use Addressables to get more control over loading and unloading assets at runtime.
You can use AssetReference
variables to hold a reference the assets you want to load, without actually loading them until you ask them to load. Then you're not tied to a specific string that might change when you move the asset around or rename it. And the asset can be in the build, in a separate asset bundle, or even downloaded from a remote server, and your consuming code doesn't need to know the difference.
I'd recommend phasing out use of the Resources
folder entirely. It's very easy to inadvertently bloat your build with this folder, because every asset inside will always be included in your build, even if nothing ever uses them. Addressables solve all the same problems, better.
I have the same question regarding variables used within runtime. Would it be better to run through the Static class that is holding all these prefabs through the Resources folder?
For this type of thing, I like to use a ScriptableObject
[CreateAssetMenu(fileName="SharedData.asset", menuName="Shared Data")]
public class SharedData : ScriptableObject {
public Transform someCommonlyUsedPrefab;
public float someCommonlyUsedValue;
// ... etc.
}
This will let you right-click in your project folder and create a new asset that acts as a data container whose contents you can edit in the Inspector.
Your other scripts can hold a reference to it the same way they'd reference a prefab, (or you can make a static getter or resource locator to look it up) but you don't need to create a GameObject
for it to sit on. It persists between scenes like static
data, but unlike static
classes or variables, it's easy to use the Inspector to review or edit its current contents.