How to unload and reload sprites into memory?

Lets say I have a sprite on my project folder. Technically a texture2d? size(100mb)

And on my scene I have a reference to it assigned on the inspector:

public Sprite s;//assigned in inspector

This should mean that if I am that scene with this reference then the sprite is loaded and is occupiying 100mb of ram, right?

Now I can do:

s = null;

Resources.UnloadUnusedAssets();

This should clear the 100mb sprite from memory?

But now, how can I get the sprite back since I no longer have a reference to it?

Let me know if there is any loophole in the previous logic.

Also how would it impact if the sprite in question was packed in an atlas?

Thanks for the help

If you create a prefab of the sprite, drag that prefab into the script in the editor, you can instantiate and delete it whenever you want. But what you probably should look into is Addressable Assets.

so no way to access the assets that are already packed into the build? the resources folder exists outside of the build

Well you nulled out your reference to it, so how do you expect to find it again? You have to load it dynamically in some fashion, which cone be done through either Resources or Addressables.

This isn’t accurate. Assets in resources just go into their own special asset bundle at build, accessible through the Resources API. They’re a part of a build like any other asset, just one you have some degree of granular access to.

Streaming assets is the folder you want if you need files to be kept outside of a build, but it comes with the major downside that you’re now responsible for loading those files in at runtime. Which might not sound like a problem but it is thanks to the fact that most of the code that would handle it in the editor isn’t available at runtime.

There are other headaches involved too like having very specific access requirements on some platforms that are very difficult to find information on thanks to how often the platforms change.

thanks, so unless I store an asset outside the build I cant retrieve it

So any asset that was compiled normally into the build becomes impossible to retrieve if I clear the reference? its ok if yes

and theres no way to have a reference to something without it consuming ram?

Any asset stored in a Resources folder will be included/packed in the build.

When you do Resources.Load(“filename”) Unity will simply load the file into memory and return a reference to it.

The resources folder exists as a way of including assets in a build without having to make a reference to them from within a scene. Sometimes developers only want to load assets when necessary as it can be a great way of reducing loading times.

Keep in mind that loading assets while a game is running can cause stutter, especially if you’re loading a large texture or mesh into the GPU. So this is why Unity is biased towards loading meshes and textures at the start of a scene and stuffing them into the GPU.

Addressables has been mentioned twice.

Just dropping this as an Addressables example.

using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Events;
using UnityEngine.ResourceManagement.AsyncOperations;

public class AddressableSpriteAssetLoader : MonoBehaviour
{
    [SerializeField]
    private AssetReferenceSprite SpriteAssetReference;

    [SerializeField]
    private UnityEvent<Sprite> SpriteLoaded;
   

    public Sprite SpriteAsset { get; private set; }

    private void OnEnable() => Load();

    private void OnDisable() => Unload();

    private void OnAssetLoadCompleted(AsyncOperationHandle<Sprite> handle)
    {
        SpriteAsset = handle.Result;
        SpriteLoaded.Invoke(SpriteAsset);
    }

    public void Load()
    {
        // Check if already loaded
        if (SpriteAsset != null)
            return;

        SpriteAssetReference.LoadAssetAsync().Completed += OnAssetLoadCompleted;
    }

    public void Unload()
    {
        // Check if already unloaded
        if (SpriteAsset == null)
            return;
       
        Addressables.Release(SpriteAsset);
    }
}

I’ve already implemented this and yet I’m still seeing a massive spike on the CPU from Loading.ReadObject which is trying to read the sprite into memory even though this should’ve already read the sprite into memory so it’s not working at all

Implemented what exactly?

The whole point of this thread was to reference assets without them being immediately loaded into memory.