Search Unity

Lazy loading textures into memory?

Discussion in 'General Graphics' started by noamgat, Apr 10, 2016.

  1. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    I have a scene with quite a lot of textures in the canvas, but most of them are off 99% of the time (think of "level up" screen, which happens around once a week for the player)

    In order to save memory, I would like unity to only load the texture when it will first be displayed, rather than when the scene is loaded. Is there an easy way to do this? I would like to do so without ruining the editor WYSIWYG experience.
     
  2. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    Update: it seems that even placing a pointer to a texture in a disabled gameobject will cause the texture to be loaded into memory when the scene loads. Is the resources directory approach the only way to solve it?
     
  3. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    An alternative would be to use the multi-scene editor / scenemanager and split off the level up screen etc into seperate scenes and load/unload them as needed (on top of the 'general' screen). There's a delay as it fetches the scene from storage though, unsure what loading times the scenes would get on mobile (possibly quite quickly as a popup scene would be small).
     
  4. dnnkeeper

    dnnkeeper

    Joined:
    Jul 7, 2013
    Posts:
    84
    I'm looking for the same. I need some complex pages to load into specific scrollable containers. Using addittive scene loading seems not to be the easiest way around because I need to change rect transforms parent and attach it to specific scrollable container after loading. Or can I use prefabs and resource load async to attach them where I need? I hope it will load textures with it asynchronously.
     
  5. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    I would recommend Assetbundles for loading memory intensive assets if you need them.
     
    barlieuy, dnnkeeper and eXonius like this.
  6. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    247
    Does using the resource folder preent assets from being loaded into memory? I am having the same issue and I am going to try retrieving my loading screens by using resource.load as they are needed
     
  7. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    983
    No. In fact it does the opposite. It force-loads everything in resource folders at startup. Have a look at the Addressables System if you want to have better control of what is loaded and when.
     
  8. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    247
    So lets say I have a store where the player can customize their character (which I do):
    I handle this by putting all materials/gameobjects in the resource folder. When a scene loads, the game loads the needed assets from the resource folder, resource.Load(), once this is finished (once materials and objects are added to the character) I call resource.unloadunusedassets()

    Doesn't this unload the assets from memory? I don't mind if the resource folder effects scene loading, I just don't want it to effect runtime performance due to textures being loaded in RAM.

    Am I not understanding how the resource system work? How is addressables any different? Why was resource ever even used? Does my system not unload the unused textures?
     
  9. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    983
    You know, I might just actually be hallucinating what I thought I had seen before. Or maybe I just misinterpreted it. either way it seems that it DOES increase startup time due to an extensive tree building step when your app begins. It likely also increases load times for assets as well as memory usage. It will also certainly increase your build size and require you to manually audit what files end up in the final build or not. Finally, Addressables can drastically speed up the final build process as well as allow for more flexibility for engine upgrades or asset upgrades and patches for your end users. Overall they are a win in every category over using the Resources folder.

    Anyway as specifically for your question: Are you sure you are referencing your asset correctly? For example, are you sure you are passing the correct datatype and referencing the name of the asset relative to the resources folder. Make note ALL assets get dumped into a single resources folder at runtime. Also are you accessing them by the correct name? IIRC you don't want to have the file extension in the name as that information gets stripped off during the build process.
     
  10. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    247
    Are you saying that you were wrong in stating that all assets are loaded into memory?

    I have a resource folder that has different sub folders (skins, hats, etc.) When I access something I use an ID which is saved in an encrypted JSON ex. [skin: "S001"] I then have a dresser script which checks the first letter of the id (in this case 'S') All store items ids are based on what they are S means skin, H means hat,

    So in the instance of loading skin "S001" the dresser script calls:
    Material skin = Resource.Load("Skins/S001");
    and applies the material to the player.
    Next I call resource.UnloadUnusedAssets();

    Everything works fine and performance is great (this game looks/performs better than 99% of mobile games) Im just wondering if every other texture/material is loaded into memory still. (All the other skins).
     
  11. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    Assets that are referenced by scene in direct or indirect way are all loaded into memory when scene is loaded.
    Resource folder content is not loaded automatically since you are loading it by string at runtime.
     
    angeldevelopment likes this.
  12. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    Just wanna add that there is also texture streaming to deal with this kind of problem.
     
    fffMalzbier likes this.
  13. BrianJiang

    BrianJiang

    Joined:
    Jun 11, 2017
    Posts:
    7
    I wrote a tool that allows you to reference assets in MonoBehaviour or ScriptableObject but delay the loading until you actually use it. You can check it here: https://github.com/Brian-Jiang/SmartReference
    Though you have to set your load function when the game start