Jump to content

[PIXI.SPINE] Problem with unloading spine2d assets.


jakubdev
 Share

Recommended Posts

Hello,

When I switch from battle system I try to unloadAll Assets. My code seems to destroy all textures the one's from spine2d, mp4, png. For some reason spine2d assets are still in garbage resulting in having 2.3gb of ram usage instead of aroung 500mb when only UI is loaded. My code works for sure for other assets, as when menu is loaded it takes about 550mb of ram and when unloaded all assets it goes down to 250mb.Spine2d assets for menu are still there as game at start takes 150mb of ram.

 

// inside function that is triggered on back button click
destroyContainer(battlePrepareContainer, true);
unloadAssets(_.flatten([(Object.values(firstToLoad)), (Object.values(transitions))]));


export const destroyContainer = (containerName: Container | string, full = false) => {
    const destroyOptions = {
        children: true,
        texture: full,
        baseTexture: full,
    };

    const destroySpine = (spine: Spine) => {
        spine.spineDebug.debugGroup.destroy(destroyOptions);
        spine.spineDebug = null;

        spine.removeAllListeners();
        spine.parent.removeChild(spine);
        if (spinePool.has(spine.name)) {
            spinePool.delete(spine.name)
        }

        console.log('spine', spine);

        spine.destroy(destroyOptions);
        spine = null;
    };

    const destroyChildrenRecursive = (container: Container) => {
        container.children.forEach((child) => {
            if (child instanceof Spine) {
                destroySpine(child);
            } else if (child instanceof Container) {
                destroyChildrenRecursive(child);
            } else {
                child.removeAllListeners();

                child.destroy(destroyOptions);
                child = null;
            }
        });
    };

    const destroyContainerAndChildren = (container: Container) => {
        destroyChildrenRecursive(container);

        container.removeAllListeners();

        container.destroy(destroyOptions);

        container = null;
    };

    if (typeof containerName === 'string') {
        if (window[containerName] && window[containerName] instanceof Container) {
            destroyContainerAndChildren(window[containerName]);
            window[containerName] = undefined;
        }
    } else if (containerName instanceof Container) {
        destroyContainerAndChildren(containerName);
    }
};

export const unloadAssets = (exceptions: loadObject[]) => {
    const cacheKeys: string[] = Array.from(Assets.cache['_cacheMap'].keys());
    const exceptionNames = exceptions.map((exception) => exception.name);

    const menuKeys = cacheKeys.filter((key: string) => {
        if (!key.includes('common') && !key.includes('assets/') && !exceptionNames.includes(key)) {
            return true;
        }
        return false;
    });

    void Assets.unload(menuKeys)
}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...