App.assets.loadFromUrl getting progress

Hi,

When loading assets directly from the engine using loadFromUrl() is there a way to get a callback with the download progress? So to present some loading progress info to the user?

Best regards,
Leonidas

Generally you don’t load everything simulaneiously. For instance, I load 9 objects for my game As a result, I have roughly 10% increments on loading objects.

The code Iuse is:

requiredobjects = []
loadedobjects = {}
loadstate = 0

function requireobj(url, folder){
    if (typeof(folder) === 'undefined') {
        folder = 'Assets/Models/'
    }
    //Will load the specified object before the game loads
    requiredobjects.push([url, folder])
}

function loadobj(){
    //Load all the playcanvas objects
    if (loadstate == 0){
        loaderdisplay('Loading Objects')
        canvas = document.getElementById("application-canvas")
        app = new pc.Application(canvas)
        app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW)
        app.setCanvasResolution(pc.RESOLUTION_AUTO)
        setTimeout(loadobj, 0.1);
        loadstate ++;
    }
    else {
        for (var i = 0; i < requiredobjects.length; i++) {
            url = requiredobjects[i][1]+'/'+ requiredobjects[i][0] + '.json'
            app.assets.loadFromUrl(url, "model", function (err, asset) {
                loadstate++;
                console.log('loaded: '+asset.name)
                if (loadstate === requiredobjects.length+1) {
                    loaderdisplay('');
                    setTimeout(loadSounds, 0.1);
                } else {
                    loaderdisplay('Loading Object '+loadstate+'/'+requiredobjects.length);
                }
            });
        }
    }
}

To add request an object you run requireobj(‘obj_name’) before you run loadobjects()
Then the function loaderDisplay gets called with a string of the load percentage
In my case, after loading the objects I load sounds, hence the call "setTimeout(loadSounds) for when it’s finished.

Warning: This code sometimes causes issues and doesn’t load one of the objects. I am unsure why, it just never makes the request to the server. At some point I will make it more robust.
You can see my loading screen/game over here.

Created a ticket for that, but seems it will be a bit low priority.
If you are brave enough, you can make clean implementation, and make a Pull Request.
as @sdfgeoff mentioned, if you load multiple assets, then it does not worth tracking loading of individual asset, rather tract overall loading progress, and you can calculate how much each asset contributes to % based on size of an asset, which is available data in advance on asset.file.size.

Thank you very much for your replies.

Best regards,
Leonidas

1 Like