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

Question Sprite loading efficiency

Discussion in 'Editor & General Support' started by scryedzxp, Jun 28, 2023.

  1. scryedzxp

    scryedzxp

    Joined:
    May 14, 2013
    Posts:
    47
    Let's say I have 10 buttons, and for each of them, I assigned them the same sprite image by dragging the image into each of the button's inspector panel.

    During app runtime, does Unity load the same image into memory 10 times or does it just load it once and reference it the other 9 times? I'm assuming it does the latter but I don't know for sure. Or should I just manage it myself with Resources.Load() through a script?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    The one sprite is loaded into memory as a dependency of the prefab or scene is loaded as well, and all the
    UnityEngine.Object
    references are pointing to the same asset. Kinda has to work this way as comparing equality between Unity objects would break.

    The only time assets get duplicated is when Addressables or Asset Bundles are involved and aren't used properly.
     
  3. scryedzxp

    scryedzxp

    Joined:
    May 14, 2013
    Posts:
    47
    Is that only for when we're assigning sprites via the inspector? What about calls to Resources.Load() on the same asset multiple times? Do I have to manage redundant loads myself?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    No you're still getting the same asset. Once again, duplicating the asset would cause equality checks to fail and be a nightmare for all sorts of reasons. Not to mention a pointless waste of resources.

    Even with addressables, multiple asset references pointing to the same address being loaded will all return the same asset.
     
    scryedzxp likes this.