Function to get a texture from Pixi's resource loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "app" is an instance of PixiJS Application | |
export default app => (filename) => { | |
const { | |
resources, | |
} = app.loader; | |
const texture = Object | |
.values(resources) | |
.filter(resource => resource.textures) | |
.flatMap(resource => Object.entries(resource.textures)) | |
.find(([key]) => key === `${filename}.png`); | |
if (!texture) throw new Error(`Texture "${filename}" not found.`); | |
return texture[1]; | |
}; | |
// Example usage, assuming you have an image called 'player': | |
// const texture = getTexture(‘player’) | |
// const sprite = new Pixi.Sprite(texture) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment