[SOLVED] Import Scripts from Server

Hello PlayCanvas Developers,

I’ve recently taken up coding a browser-extension to use with Play-Canvas. This is something a client has requested to make creating certain applications faster etc.

Now I’ve been able to create extra menus for adding entities etc. but what would be really awesome, is if I could do something like the “import Ammo” Button. Where one or multiple scripts are added to Project-Assets so they can be used when creating “Prefab” entities with the extension or just added manually to entities. For example a orbit-cam.js which would be imported when clicking the button.

I’ve dug through the editor.js and found, that the ammo.js is imported from the playcanvas Store. So I don’t think I can load it from there because to add the directory and scripts to the project they call https://playcanvas.com/api/store/somenumber/clone which then clones the “somenumber” into the directory of the submitted project. Seeing as I don’t think I can upload anything to their store this is not likely an option.

I thought of just trying to do it like if someone would drag and drop a file into the editor, but haven’t been able to successfully locate how the file is uploaded and added.

Any tips from the Developers on how this could be possible? Or are there security issues? the files would have to be provided in a secure location of course. Otherwise they might be altered. But the browser-extension would only be given to a specific client, the scripts uploaded to a and downloaded from a secure amazon cloud server.

Thank you in advance!

Hi @Bfischlin,

You can do that easily using the following piece of code from your extension injected Javascript, in the Playcanvas editor window (or just the browser dev console):

editor.call("assets:create:script", {
    filename: "my-script.js",
    content:
`var MyScript = pc.createScript('myScript');

MyScript.prototype.initialize=function(){
    console.log('Downloaded from an extension to Playcanvas!');
};`
  });

Many thanks to the Playcanvas team for making the Editor insanely extensible! @will @vaios @moka

2 Likes

ahh I see!

So I just pase the code as string inside! That works!

Thanks a lot :smiley:

1 Like