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. Dismiss Notice

How does Resources.Load work internally

Discussion in 'Scripting' started by ThomasVandenberghe, Nov 7, 2014.

  1. ThomasVandenberghe

    ThomasVandenberghe

    Joined:
    Feb 28, 2014
    Posts:
    22
    Since you can have multiple Resources folders in your project, I'm wondering how Unity would know the difference between two assets that have the same relative path.

    For example:

    Assets/Folder A/Resources/Subfolder/file.txt
    Assets/Folder B/Resources/Subfolder/file.txt​

    When I would use

    Resources.Load<TextAsset>("Subfolder/file");​

    Could I know which file it will load? It doesn't seem to be random since it's loading the same file each time.
    Could I force it to load the other file instead?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    I don't know how it works internally, most of us won't, because that is done all on the unmanaged side and is only available to be seen by those who have source code licenses (or work for Unity).

    I will say that Resources.Load will select the first one that it finds with a matching name. If you specify the type it'll be the first one of that type it finds that matches that name. If two assets have the same name and type, then it's the first again. It's always the first.

    The order of the assets though that would make which 'first' is unknown.

    Don't have 2 assets of the same name/path and type!
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    While the engine *does* support it I've generally found it to be a bad idea to have multiple Resources folders :) Just feels like setting myself up for obscure failure.
     
    lordofduct likes this.
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Very much agree with KelsoMRK.

    I sort of see different resources folders as an option for 3rd party plugin creators to include resources with out having to tell the user to put certain files into their root Resources folder. But to still have some unique folder in it to id themselves.

    Plugins/LordofductPlugin/Resources/Lordofduct/...
     
  5. ThomasVandenberghe

    ThomasVandenberghe

    Joined:
    Feb 28, 2014
    Posts:
    22
    Yes I agree with you guys, it's just that I've seen code where someone would use multiple Resource folders together with LoadAll, which runs fine then.

    But multiple folders with single Load is indeed doomed to fail at one point.
     
  6. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    Still, it would be nice to know the logic that decide which file to load, because it can definitely be of use.

    For example, if it loads the one with the latest "modified" field in the meta, it can be used as a cheap way to mod resources.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    "cheap way to mod resources"

    ???

    What do you mean?

    You'll be getting resources included in your build that you want be loading (the 2nd, 3rd, 4th of the same name and type). That doesn't sound cheap to me.
     
  8. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    No, what I mean is that I did something a bit similar in an old XNA project, where it would load from the base resources only if there was not the same name in an other folder.

    What it allows is for a pretty easy way to override textures, dialogues or models by the final user by simply putting a file in a folder and not having to worry about getting/making an actual mod for the game.

    I'm pretty new to Unity, but that question made me remember what I did back a few years ago as a student.
     
  9. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Correct. If you have two (or more) assets with the same name, then you need to write some runtime code that can work out which one you want to use. So, to use the example in the OP, you might need a first line of your file.txt that helps you work out which asset has been found. And, I think, using LoadAll is probably the only way you can get all of them anyway.
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That'd work if you had access to the individual files post-build, which you don't. So any folks doing the overriding would have to have Unity and download your full project anyway.
     
  11. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    Too bad, but that's a good thing to know.