Replacing texture atlas at runtime?

Hello All,

I’m trying to replace a texture atlas at runtime. The idea:

  • Having a default atlas setup in editor with preload to false.
  • When I start my app, I’m looking at specific location if a replacement exist (ex: ../path/image.png).
  • if no replacement, just load default atlas.
  • if replacement, not sure what to do here
  • file is same size, with same sprite zones.

If I replace it directly in the file system folder (files/XXXXX/1/image.png), it does work. But would like to do it by code.

Thank you for you support.

Mykel

Hi @memleak,

I haven’t done this in code, but I am thinking that something like this may work if you would like to test:

app.assets.loadFromUrl('../assets/textures/newImage.png', 'textureatlas', function (err, asset) {
   // now use the new asset received in your sprites
   spriteAsset.resource.atlas = asset.resource;
   // or on your SpriteComponent
   myEntity.sprite.sprite.atlas = asset.resource;
});

Thanks, Here what I ended up doing

app.assets.loadFromUrl(url, “texture”, function (err, asset) {

    //load atlas if no override is found
    var assets = app.assets.findByTag(tag);
    var assetsLoaded = 0;
    var assetsTotal = assets.length;
    var atlas = assets[0];
    
    if(err !== null && err !== undefined) {
        console.log('No replacement texture found for ' + tag + ' atlas!');   
        
        //load atlas if no override is found
        var onAtlasReady = function() {
            self.onAssetsLoaded();
        };

        atlas.ready(onAtlasReady);
        app.assets.load(atlas);
    } 
    else {
        console.log('Replacement texture found for ' + tag + ' atlas!'); 
        
        // Callback function when an asset is loaded
        var onAtlasReady2 = function() {
            atlas.resources[0].texture = asset.resource;
            self.onAssetsLoaded();
        };

        atlas.ready(onAtlasReady2);
        app.assets.load(atlas);
    } 
});

it works really well!

The problem I have now, when Im looking for a file using loadFromUrl and is not found, I get an error. I it possible to disable it? I tried to try/catch it, but I always get the error. Maybe loadFromUrl is not the right way to go? Its a valid case for me to not find a file if it wasnt overriden.

Thanks

1 Like

What error are you seeing (screenshot please)? I can’t see anything in the code that prints or throw an error in the engine code at a glance.

My understanding is that errors are trigered when loadFromUrl is used and nothing is found on location.

In my case, If I dont find anything to be used as an override (texture and localization), I fallback to default. So I would like to disable the errors.

Do the errors matter if they are in the console log only? They are all handled gracefully in the Engine.

The ‘Failed to load resource:…’ is from Chrome/browser. The engine can’t do much about that.

The other one may be in the engine but can’t find it right now.

You can do an extra XMLHttpRequest to see if the remote file exists but that won’t get rid of the Chrome error message: https://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-pure-javascript

A REALLY hacky workaround is to monkey patch the console.error function.

Thank you so much! I can live with the error mesage in the log window

In which case, I would leave it as is. On the published builds, those errors don’t show up on screen unlike the launch window.

1 Like