How to cancel AsyncOperationHandle?

Hi,

I’m trying to setup a simple use case for using addressables.
I have a UI with pages, Each page is an addressable asset.
When the player presses the right and left key I want to fade in the next or previous page.

But if the player presses the keys very fast, I want to fade out put a loading icon until they stop and then fade in.

So to keep this efficient I want to load the current page and cancel if the page index has changed before the load was complete.
This way I don’t have to load 50 pages concurrently just to unload them as soon as they are loaded if the player presses a key very fast.

My initial naive approach was this, and as assumed it loads a lot of things at the same time, and sometimes even doesn’t unload properly

private void LoadContent(int index)
    {
        m_AsyncOperationHandles[index] =
            m_References[index].InstantiateAsync(m_ContentParent, false);
        m_Loaded[index] = true;

        for (int i = 0; i < m_Loaded.Length; i++) {
            if (i != index && m_Loaded[i]) {
                m_Loaded[i] = false;
                m_References[i].ReleaseInstance(m_AsyncOperationHandles[i].Result);
                if (m_References[i].IsValid()) { m_References[i].ReleaseAsset(); }
            }
        }
    }

I looked at the documentation but couldn’t find anything about cancelling the operation before it is complete

1 Like

Try Addressables.Release(_asyncOperationHandle);