fernfreak

Load all assets in a folder

Recommended Posts

I was looking through the Loader class, and it seems that in order to load an asset, I need to name each file, like in:
loader.add('example-sprites', 'example-sprite-sheet.json');

Is it possible to load all of the files in a folder without specifying each one by name? I know that I'd like to cache all the files within a specific folder, but I don't actually know what the filenames are prior to loading. 

If this isn't possible, what would be the best practice for this sort of situation when the files aren't known?

Really appreciate any advice on this-- thanks in advance! :)

 

Share this post


Link to post
Share on other sites

you need use a low-level environment for running server-side JavaScript.
take a look on the module node.js. "nwjs"

It allow you to scan your systems files and make a files list for the loader.

http://www.monitis.com/blog/6-node-js-recipes-working-with-the-file-system/
and the official doc, but not completed.
http://docs.nwjs.io/en/latest/

Share this post


Link to post
Share on other sites

How about a script like this:

var fs = require("fs");

var list = fs.readdirSync("game/static/assets/audio");

    var dynamicAssets = "{\n  \"paths\":\n  [";
    for (var i = 0; i < list.length; i++)
    {
        dynamicAssets += "\n    \"/assets/audio/" + list[i] + "\",";
    }
    dynamicAssets = dynamicAssets.substr(0, dynamicAssets.length - 1) + "\n  ]\n}";

    fs.open('build/dynamicAssets.json', 'wx', (err, fd) => {
        if (err) {
            if (err.code === 'EEXIST') {
                console.log('dynamicAssets.json already exists');
                return;
            }
            throw err;
        }
        fs.write(fd, dynamicAssets);
    });

 

and then you load them like this:

 

let bootLoader = new PIXI.loaders.Loader();

bootLoader.add('dynamicAssets', 'dynamicAssets.json?version=' + version)

let dynamicAssetPaths = this.app.bootLoader.resources.dynamicAssets.data.paths;
        for (let assetIdx=0; assetIdx < dynamicAssetPaths.length; assetIdx++)
        {
            let assetPath = dynamicAssetPaths[assetIdx];
            let regExp = /([^\/\\&\?]+\.\w{3,4})/g;
            let assetName = regExp.exec(assetPath)[1];
            PIXI.loader.add(assetName, "." + assetPath + "?version=" + version, {loadType:"XHR", xhrType:"blob"});
        }

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.