Replace element sprite texture atlas

Hi there,

What I am trying to do is dynamically load external spritesheets and replace current texture atlas.

Loading and replacing is only successfull on sprite components.

I am wondering why this won’t work on sprites assigned to an element component.

// initialize code called once per entity
ReplaceTextureAtlas.prototype.initialize = function () {
    this.entity.button.on("click", function () {
        // replace sprite atlas texture
        this.app.root.findByName("Sprite").sprite._clips['Clip 1']._sprite._atlas._texture = LoadVisuals.awardWin.resources[0];

        // replace element sprite atlas texture
        this.app.root.findByName("Element Sprite").element._image._sprite._atlas._texture = LoadVisuals.awardWin.resources[0];

        console.log(this.app.root.findByName("Element Sprite").element);
    }, this);
};

Here’s a test project to show what is (not) going on.
https://playcanvas.com/project/1058525/overview/replace-sprite-textureatlas

What am I missing?

Thanks.

Hmm, I wouldn’t do it at the component level but rather at the sprite asset level, let me take a look at the project

1 Like

Example: https://playcanvas.com/project/1058576/overview/f-replace-sprite-textureatlas

1 Like

This is a more efficient way indeed!

I will have to implement it in the old LoadVisuals script in the original project as there are a lot of assets being loaded depending on the user.

Thanks @yaustar!