Dynamic Asset Loading


#1

Hello dear Community and PlayCanvas Team,

i would like to know if it is possible to download Assets during Runtime that haven´t been set up as Assets in the Project.

For Example:

  1. User Clicks a Button
  2. Application Downloads 3D-Model from www (CORS is considered)
  3. A Script ckecks its name and looks into a JSON File that Contains Information about what kind of Components should be added
  4. Components get added to the Model
  5. Model gets rendered

Is this possible or do all Assets have to be set up in the Editor before, with “Preload” unchecked?

Regards,
Markus


How to load a model from json and add it to the scene by code
#2

Yep. This is totally possible.

Something like this should work:

var asset = new Asset("Remote Model", "model", {
    url: "http://example.com/remote-model.json"
});
app.assets.add(asset);

asset.on("load", function (err, model) {
  //loaded
});
app.assets.load(asset);

#3

Hey Dave,

thanks for the fast reply. I´ll give that a try :smile:


#4

Hi Dave,

is there some Tool to Convert 3D-Models into the JSON-format that PlayCanvas uses?


#5

You can download models from your scene in the Editor. Select the model and click “Download” in the right attribute panel.

This will give you a zip file containing the model .json file.


#6

Hey, I have a question. Will this still work if i don’t have a file url?

In my project, we receive the JSON with a GET (so the info is transmitted from a web page), so we don’t actually have a file.


#7

@Linna_Lim See: How to create/load asset from Json variable (not from file or url)?