Resources folder and asset loading etiquette

I’m curious. Being that Unity uses the resources folder to manage which assets are included in a build (as far as I know), how do people manage assets that come from outside packages, which are normally placed by default in the root when imported?
Assets such as included prefabs.

In the project i’m working on, a current workaround for including assets in the build, assets from various packages, is to duplicate the included prefabs and place the duplicates in the resources folder. But it’s very counter intuitive for various reasons. A cleaner way would obviously be to reference the original prefabs, but then, to achieve the same result, wouldn’t the entire package from which they originate have to be placed in the resources folder?

I’d love to hear how people are tackling this. Pitfalls, pros and cons and what have ya.

Unity includes all assets from the Resources folder whether they are used or not. In general you should avoid it unless you have a specific reason. Unity works better if you reference objects directly. Don’t duplicate stuff like that; there’s no reason for it.

–Eric

2 Likes

No, it tracks references internally to do that. It’ll figure out what a build needs all on its own regardless of where items are in the Assets folder (ie: your Project view). Duplicating stuff as you are isn’t helping anything. In fact, Unity will assume you’ve deliberately made two different versions and behave as such.

Using a Resources folder (as @Eric5h5 says) forces Unity to include those assets, and makes them available via Resources.Load(…) et al.

In addition to what Eric said, any object referenced is included no matter where it is. (In the project)

D’oh! Beaten by the penguin

1 Like

It does go without saying that the duplication is completely redundant. A major flaw in doing so is over-complicating the workflow by requiring you to update more than one copy of the same object. But the reasoning is, not wanting to move the entire asset folder containing more than just the prefabs needed, into the Resources folder.

For example if say, I imported a package of buildings and trees, as well as scripts that manage their level of detail or occlusion culling.
I want to include a handful of prefabs from the package, but not nothing else (aside from the assets referenced by the prefabs). And lets for the sake of example assume that about 70% of the assets in the package aren’t used at all. And assuming that for this to be possible, I would have to place the entire package folder into Resources, wouldn’t that again be including a whole lot of unused assets?

Not to mention the perceived necessity of physically moving the imported asset folder into Resources. As opposed to keeping it in the root. With a lot of on hand assets, the Resources folder is sure to fill up with assets that will possibly one day be replaced.
Personally I would have thought a more practical solution would be to mark the imported assets folder as a resources folder and keep a reference to where it sits. And changing the reference in the future if the package is replaced by something else.
You’d still be loading unneeded assets, but hey, no need to physically move it around and clutter up the Resources folder.
I have no idea if this is even possible given how Unity manages Resources, but it’d be curious to see.

Again, this is more of a shoot-the-sht question than a crisis on my behalf. I’m sure people each have their own workflow and structure for dealing with this. I wouldn’t mind gaining a bit of perspective of different circumstances and solutions thereof.

That’s true. I did assume that as well. Such as references to gameobjects in scenes or values set in the inspector.

I should probably have made it clearer that my primary curiosity lies with the Resources.Load function, and loading assets that do not have initial references to them. Such as say in procedural generation. Or any in situation where only some of the assets may be used, or all. Depending on actions in game.

If you need to use Resources.Load(…) on something then move your one copy of the asset to somewhere in a Resources folder. That’s the only thing you have to do. Don’t make a duplicate, just move the one you already have.

Note that Resources folders can be anywhere (you’re not limited to one), and they can have sub-folders (so you can still organise stuff).

1 Like

It sounds to me like you’re over-complicating things. What is the reason for moving the asset folder, or anything in it, to the (or a) Resources folder in the first place? And there’s definitely never a reason to make duplicates of anything.

–Eric

Here’s a crude drawing of what I had in mind.

There’s still no indication of what you’re trying to gain by doing any of this. Unity manages everything by itself just fine as long as you keep stuff out of Resources, so you’re just making work for yourself and losing automatic asset management.

–Eric

1 Like

You can also make the resources folder a sub folder of the existing structure.

But if you are going to drag items around anyway it might be better just to make a script with an array of references.

1 Like

The resources folder is only for things where you need to access things by file system path. Normally you shouldn’t need it. Reference the prefab.

ScriptableObject is a great way of doing this.

1 Like

In defence of the resources folder, its worth talking about good ways to use it.

I use it commonly for procedural stuff. Say I build a character selection menu. I have no idea how many characters I am going to use. But if I make a folder called Resources/Characters then I can throw in any character I build into this folder, and have the menu load them dynamically at runtime. I do not have to worry about registering them.

Same sort of logic goes for things like terrain tiles or obstacles or the like.

1 Like

For that stuff I built a small tool that lets me pick a folder and type and store them in an array associated with the folder name. I then have menu command to “register assets”. Which stores what is present.

In simple usage it does as you describe. As a bonus, it makes sets. I use it fo Ui skinning mostly. Say I have a set of Ui images, I can have a “blue” folder and “red” folder with contents havering the same name in different files.

3 Likes

That’s exactly the reasoning behind my wanting to use the Resourches folder in this large a capacity. The procedural assets available in my project number in nearly one hundred.
And as such, it is difficult to register everything in the manner a lot of people suggest by moving assets to the Resources folder.

Any solution that would utilize Unity’s auto asset management and still allow for this kind of mass inclusion would be invaluable.

Here’s one :wink:

AssetBundles.
Allows you to alter the content of a game without recompiling (as long as your internal logic is sound).

2 Likes

Exactly, I was about to mention that.

1 Like