Retrieve Assets by Path

Hi all,

In an extension of this thread, I’ve decided that I want to create an array of objects procedurally on initialization. This would involve loading in assets by their path, for which I found pc.app.assets.getByUrl(). A compact little function which according to the documentation should work like var asset = app.assets.getByUrl("../path/to/image.jpg");.

Now, I’ve got my file structure similar to something like
/ (Root)
|–Art
|–Scripts

and so I expected something like pc.app.assets.getByUrl("../Art/textureFile.png"); to work. Cutting to the point, it apparently works instead like pc.app.assets.getByUrl("assets/files/Art/textureFile.png?id=[asset id]&branchId=[branch id]"), which seems strange, given that the asset id is an 8 digit number that I have no way of knowing without spending time hunting for it, and the branch id can very well change each time I create a new branch.

Am I missing a simpler way to do this? The other way I can think of doing this is to cycle through the entire asset registry, looking for a particular snippet of “/Art/SpecificFolder/” and then pulling and filtering those files, which will undoubtedly end up messy.

You can get a specific branchID for a script by intentionally causing an error, then in the launch page it comes up with something like this

Hi @TingleyWJ,

Indeed the assets folder structure doesn’t reflect in any way in the asset serialized info. So you can’t get that in coded.

You will have to use other means to identify and find your assets like their names or tags, or references to script attributes.

As Leonidas has mentioned, the way that PlayCanvcas Editor stores the assets on the server is different to how it is seen in the asset panel.

What I would do in this case, rename the assets so that when sorted, they would be in the order that I need them and give them a tag. Call assets.findByTag and to get an array of he assets and sort them by asset name.

We are in the spec stage of creating a more comprehensive script attributes system, but that’s a way off at the moment.

1 Like