Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Get folder of asset inside a Resource folder

Discussion in 'Scripting' started by Sabo, Jan 22, 2017.

  1. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    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?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    I looked into this a little while back, and unless something has changed since then... no, it is not possible.
     
    el_Guero likes this.
  3. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    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.
     
  4. kor6k

    kor6k

    Joined:
    Jan 8, 2017
    Posts:
    50
    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!
     
  5. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    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.
     
  6. kor6k

    kor6k

    Joined:
    Jan 8, 2017
    Posts:
    50
    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?
     
  7. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    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.
     
  8. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    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?
     
  9. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    Yes, I believe that is a possibility and something I will do for my own projects in the future.
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  11. kor6k

    kor6k

    Joined:
    Jan 8, 2017
    Posts:
    50
    How is it Superior? performances?
     
    Sabo likes this.
  12. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    What is "inspector references"?
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Performance is slightly superior. Flexibility is infinitely superior. For example you have GetAllNames, which would eadily solve your current problem.

    https://docs.unity3d.com/ScriptReference/AssetBundle.GetAllAssetNames.html

    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.
     
  14. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    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.
     
  15. kor6k

    kor6k

    Joined:
    Jan 8, 2017
    Posts:
    50
    It's funny, it seems you are doing the same game as me !!! Diablo like game?
     
  16. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    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.
     
  17. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Resources works best for super simple configurations. Once you get moderately complex, as the OP has discovered, it becomes a pain.

    You can create a scriptable object 'database' and use that.