Search Unity

Question How much should I use Resources.Load()?

Discussion in 'Editor & General Support' started by IIBearWithII, May 30, 2023.

  1. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    I read that I need to be careful because I think it said all of the Resources will be brought into each scene. If I use Resources to keep a lot of things in, would that be a problem for the average game? I'm specifically things of keep all audio in there. I know that probably wouldn't be recommended, but could I technically get away with it. I just finished a chapter on sound from a book I'm studying and learned that audio clips, especially long ones, can take up a lot of memory...
     
  2. Not the resources themselves, but the indexing table.

    I would learn Addressables instead properly and setup assets on the way they should be so they can be loaded and unloaded as needed.
    I also would rather click the audio 'streaming' option rather than resources load.

    In summary: you should aim zero
    Resources.Load()
    as hard as you can.
     
    IIBearWithII likes this.
  3. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,794
    The issues related to the indexing table are very overblown, using Resources.Load is fine. We've been using it liberally (although not more than we need) and we haven't had a problem with that aspect even on S***ty dog ass slow mobile devices.

    I'm not even sure Unity knows what the problem is, because when they produced a blog post to convince everyone to use asset bundles instead of Resources, there was barely any benefit shown (if at all).

    https://blog.unity.com/technology/asset-bundles-vs-resources-a-memory-showdown

    This is asset bundles:


    This is Resources:



    And again asset bundles but now "optimized"


    Which one uses less memory?

    Employees have also said the wrong things in the past as well.

    That said, don't put in it more things than you need. I wouldn't use it for audio really, there are other ways to manage audio related memory that don't involve the resources folder.

    Should you have your whole project in Resources? Definitely not. Should you put some assets that you only want to load sometimes in there? Absolutely.

    You should use it as much as you need it and not more.

    You can also read older discussion on the matter here: https://forum.unity.com/threads/how-to-reduce-enter-play-mode-time.484279/#post-3156006
     
    Last edited: Feb 12, 2024
    MousePods and IIBearWithII like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    THIS!!! I have used nothing but Resources.Load() in almost every one of my personal games and they run just fine, even on creaky crusty ten-year-old mobile hardware.

    The same zombie people shrieking on and on about Resources.Load() being a problem are probably related in some weird and unholy way to those howling about how coroutines are faster than Update (THEY AREN'T!).

    In fact, here's my blurb:

    Always use
    Resources.Load<T>()
    , never use
    Resources.Load() as T


    https://forum.unity.com/threads/resources-load-not-working.1137463/#post-7306567

    And the same goes for
    Resources.LoadAll<T>()


    Generally you want to avoid writing code that is simply waiting to mysteriously fail simply by the future appearance of an identically-named file of a different type.

    Maybe this explains why so many people say it is slow:

    ALSO: Always put your resources in type-ish subfolders if you use Resources.LoadAll<T>(). As of this writing, if you use Resources.LoadAll<T>("foldername/"), Unity will load ALL FOUND ASSETS at that path into memory, then check each type and only return to you the ones that meet type T. But Unity will have already loaded all the assets it finds in that path, so expect massive performance problems if you don't use subdirectories.
     
    zedz, AcidArrow and IIBearWithII like this.
  5. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    I have had only good experiences so far using Resources.Load() and the like. I just would like to understand it better. Do Assets that are in Project folders other than Resources get brought into the final executable game? I'm trying to understand what the consequences would be and why for having too many assets in the Resources folder vs too many assets in general.
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,794
    Everything in the resources gets added to you build regardless of whether you use it in the game.

    (same as with asset bundles...)

    Stuff in the rest of your folders only get added to the build if there are direct references to it in the scenes you are building.
     
    IIBearWithII likes this.
  7. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Ok, awesome! Sorry, still trying to grasp some of the basics...
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,794
    Absolutely no need to apologise for anything. And you are asking about an issue that even Unity employees are not clear on, so it's definitely something to ponder about.

    In general though, don't go crazy about best practices and trying to pre-emptively prevent problems. As long as you reasonably look into things when you use them, you should be fine. It is inevitable that along the way you'll hit some roadblock that will force you to change your approach and then you will waste a couple of weeks to change the approach, but those are the kinds of things you can't quite avoid, especially as a beginner.
     
    IIBearWithII and Kurt-Dekker like this.
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    You can think of resources as a singleton asset bundle that gets loaded on startup. As long as you don't put too much stuff there, you will likely not see the overhead.

    I remember a few years ago somebody reported a bug saying it took 2 minutes before splashscreen was displayed. Turns out they had gigabytes of data in their resources folder across thousands of files. The index for them was getting loaded at startup and there was a flaw in that code that accidentally contained an algorithm that was O(n³) (or something like that). That bug has since been fixed, but if your resources folder is large enough, you will start noticing delay before the first frame is drawn.
     
  10. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Ok, I can follow that advice. Thanks!
     
  11. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    I'd really like to be able to put all of my prefabs and audio clips in Resources. For one reason or another it seems to help me avoid null references as my program goes through creative changes. But that sounds like it will make the initial load too long. I just wish there was a way to tell Unity which Resources I will be loading for that scene, and have only those added to the "index".
     
  12. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Sorry, but what you're saying implies that all of the assets in Resources are loaded at startup, which going by this thread and several others should not be the case.

    Yes, the indexing file is created at startup, and can use RAM and cause a lag spike, but all assets are not loaded into RAM.

    Can you clarify?
     
  13. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,933
    An "asset bundle gets loaded" doesn't mean the contents of it are loaded, just that Unity prepares the look-up table for the assets within it.
     
  14. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Thanks for the clarification!