I am working on a save / load system of assets. I have put my assets in a Resources folder to be able to load them at runtime. There may be multiple sub-folders of the first sub-folders inside the Resources folder. This causes a bit of a problem, since I do not know how to get the full path of the asset.
In the folder structure below I can easily get the path of the firebolt_01 assets since it is located in the first sub-folder of Resources, that is, “Spells/firebolt_01”. I cannot get the path to weapon_01, however, since it is located two folders deep of the Resources folder (Items/Weapons/weapon_01). I rather not create a bunch of Resource folders inside each current immediate sub-folder of Resources (Items, Spells).
Resources
Items
Weapons
weapon_01
weapon_02
Consumables
apple_01
Spells
firebolt_01
Is there a way to get the full path of an asset at runtime somehow, knowing that it is in a Resources folder?
Yeah, googling told me the same. Figured I would ask here since a lot changes between each new Unity version and information that is a year old can easily be out of date. That is unfortunate. Will have to figure out a workaround then.
I had the same problem when I created my load/save system so I use a little trick to solve it:
I stored the path of the object in the “name” attribute during its creation.
so in your case, the name of an instance of firebolt_01 will be “Spells/firebolt_01” and an instance of weapon_01 will be :
“Items/Weapons/weapon_01”
It’s a little dirty but it works!
I had the same thought myself earlier. I decided against it since it would break if people moved the asset. A little bit more advanced solution would be to use that in combination with something that detects when an assets has moved and update the name accordingly. Should be possible.
It works for me because I generate the stage of my game by code.
So during the first generation of the level, I know the path of the objects I have to instanciate; I just choose one of the tiles randomly and whan the object is instanciated, I modify it’s name with the path value.
That’s why it will always work (if someone move the asset, the code used to create the stage will be modified accordingly)
Why in your case someone would like to move an asset? And how is your scene created? manually or by script?
Not saying the would move it, but the fact that they can move it is a bit of an issue. I am currently working on a tutorial and want to keep the focus elsewhere, so opting for the easy solution this time.
Perhaps you could have the editor generate the information automatically/manually? Have it generate a file/scriptable object that lists all the files in a Resources folder with full path plus any other information you need?
If you start with a false premise, you are going to end up with a false conclusion. There are two other common ways to load assets at runtime. Asset bundles and inspector references. I’d strongly suggest investigating asset bundles, these tend to be superior to resources.
You know, just drag and drop the asset into a serialised field in the inspector. Works with GameObjects or ScriptableObjects. If you’ve got a lot of complex references to set up, you can use an editor script to set them.
I do not think that inspector references is a good option since many of the references will not be set until runtime (inventory being empty at start of game for instance).
Having looked at Asset Bundles I am deciding against them in this case. They are, however, indeed something to keep in mind. Thanks for notifying me about them.
I suppose it depends on exactly what you want to do. Resources folders works great for me in certain situations - where Asset bundles would involve extra complication.