How to access and parse a .ldtk file?

Hi, I have two separate questions relative to this initial question.

Is it possible to define a generic Asset input into a component for a very specific extension file ?

In my case I would like to give support for .ldtk files (which are just big json files)

image

If for whatever reason this is not possible, can you explain how to parse that very same file after loading it with assetManager ?

My code:


assetManager.parser.register('.ldtk', function (file, options, callback) {
    // Parse the downloaded file
    callback(null, file);
});

@ccclass('LDtk')
@executeInEditMode(true)
export class LDtk extends Component {

    start() {
        assetManager.loadAny({'uuid': '5473bf97-839e-4d11-af5d-acd2e30bfaf4'}, {isCrossOrigin: true}, (err, asset: Asset) => {
            // I can just access to the file content with ._file but is private and doesnt seem the right way to do it
        });
    }
}

This file is not a TextAsset, it is an unknown type of Asset. You can change the type of the LD tk slot to cc.Asset.

Great @VisualSJ that’s what I was looking for, do you also know how to parse it ? I cannot find the right way in the API doc

    parseLDtk() {
        if (this._ldtkAsset) {
            // parse content
            const asString = this._ldtkAsset.toString();
            const serialize = this._ldtkAsset.serialize();
            const native = this._ldtkAsset._nativeAsset;
            console.log(asString); // assets/general/native/54/5473bf97-839e-4d11-af5d-acd2e30bfaf4.ldtk
            console.log(serialize); // undefined
            console.log(native); // OKAYSH but deprecated
        }
    }

I think without customization in the assetdb importer, it would be very hard to do what you what, @VisualSJ can give you walk arounds, but the correct way would be using AssetDB customization, I hope we can open it sooner

Using _nativeAsset is ok, we will add another interface nativeAsset to instead _nativeAsset in 3.8

1 Like