I notice there are some questions with the same topic, but they all use the playcanvas editor.
I am using playcanvas engine without the editor, and here is my folder structure:
├── app.js
├── camera.js
├── index.html
└── libs
└── playcanvas-stable.js
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<style>
body {
margin: 0;
padding: 0;
}
</style>
<script src="/libs/playcanvas-stable.js"></script>
</head>
<body>
<canvas id="application"></canvas>
<script src="/app.js"></script>
</body>
</html>
camera.js
pc.script.create('Camera', function (context) {
var Camera = function (entity) {
this.entity = entity;
};
Camera.prototype = {
update: function (dt) {
console.log(dt); // do not trigger.
}
};
return Camera;
});
app.js
var canvas = document.getElementById('application');
var app = new pc.Application(canvas, {});
app.start();
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
var cameraEntity = new pc.Entity();
cameraEntity.addComponent('camera');
app.root.addChild(cameraEntity);
app.systems.script.addComponent(cameraEntity, {
scripts: [{
name: 'Camera',
url: './camera.js'
}]
}); // not work.
I found most posts mention to use context.systems.script.addComponent(entity, { scripts: [] })
to add script to entity programmatically, but I am failed.
Can anyone tell me how to do it ? thanks.