Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Spritesheet option cachePrefix #10125

Merged
merged 4 commits into from
Jan 18, 2024
Merged

Conversation

bigtimebuddy
Copy link
Member

@bigtimebuddy bigtimebuddy commented Jan 17, 2024

This was a request that was made via Discord.

The use-case is that we have a global TextureCache and a global Assets cache and collisions of textures can easily happen with an app that's a little more complex. Sometimes there's a need to be more specific when loading multiple atlases that might share the same texture name. This change allows users to optionally provide a cachePrefix that prepended to texture IDs whenever adding to TextureCache or Assets' Cache.

Added

  • Adds cachePrefix option to Spritesheet
  • Adds options for Spritesheet constructor

Chores

  • General cleanup of Spritesheet tests to unload assets better

Example

import { Assets } from 'pixi.js';

await Assets.load({
   src: './atlas.json', // contains "background" texture
   data: { cachePrefix: 'atlas.json/' }
});

await Assets.load({
   src: './atlas2.json', // also contains "background" texture
   data: { cachePrefix: 'atlas2.json/' }
});

const background = Assets.get('atlas.json/background');
const background2 = Assets.get('atlas2.json/background');

@bigtimebuddy bigtimebuddy added this to the v7.4.0 milestone Jan 17, 2024
Copy link

codesandbox-ci bot commented Jan 17, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 978ae57:

Sandbox Source
pixi.js-sandbox Configuration

@@ -388,7 +428,7 @@ export class Spritesheet<S extends ISpritesheetData = ISpritesheetData>
);

// lets also add the frame to pixi's global cache for 'from' and 'fromLoader' functions
Texture.addToCache(this.textures[i], i.toString());
Texture.addToCache(this.textures[i], this.cachePrefix + i.toString());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would certainly resolve my use case of needing to namespace same-named textures in multiple loaded spritesheets, so looks good to me!

@Zyie Zyie added the 💞 Migrate to v8 This PR needs to be migrated to v8 label Jan 18, 2024
@bigtimebuddy bigtimebuddy added the ✅ Ready To Merge Helpful when issues are in the queue waiting to get merged. This means the PR is completed and has t label Jan 18, 2024
@bigtimebuddy bigtimebuddy merged commit 5ae1d3b into dev Jan 18, 2024
3 checks passed
@bigtimebuddy bigtimebuddy deleted the feature/spritesheet-cache-prefix branch January 18, 2024 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💞 Migrate to v8 This PR needs to be migrated to v8 ✅ Ready To Merge Helpful when issues are in the queue waiting to get merged. This means the PR is completed and has t
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants