camera.addComponent("script");
camera.script!.create(OrbitCamera);
camera.script!.create(OrbitCameraInputMouse);
camera.script!.create(OrbitCameraInputTouch);
The system will prompt。。。
How do i create a script without a name
script 'null' is already added to entity 'Untitled'
Hi @zhou_xia,
To add a script instance to a script component you pass a string argument with the script type name. Like this:
camera.addComponent("script");
camera.script.create('orbitCamera');
camera.script.create('orbitCameraInputMouse');
camera.script.create('orbitCameraInputTouch');
create(
nameOrType: string | typeof pc.ScriptType,
args?: {
enabled?: boolean;
attributes?: any;
preloading?: boolean;
ind?: number;
}
): pc.ScriptType;
@Leonidas
The type shown here can be passed in, I created it through typescirpt, no use ide
I am full of TS files and cannot be created as you said. There is no way to find out which classes I created by the name of this method.
pc.registerScript(OrbitCamera, "OrbitCamera");
camera.script!.create("OrbitCamera");
pc.registerScript(OrbitCameraInputMouse, "OrbitCameraInputMouse");
camera.script!.create(OrbitCameraInputMouse);
I have solved it, I will optimize it again. I just looked at the source code, which is not particularly friendly to typescript!
var createScript = function (name, app) {
if (pc.script.legacy) {
// #ifdef DEBUG
console.error("This project is using the legacy script system. You cannot call pc.createScript(). See: http://developer.playcanvas.com/en/user-manual/scripting/legacy/");
// #endif
return null;
}
if (createScript.reservedScripts[name])
throw new Error('script name: \'' + name + '\' is reserved, please change script name');
var script = function (args) {
pc.ScriptType.call(this, args);
};
script.prototype = Object.create(pc.ScriptType.prototype);
script.prototype.constructor = script;
script.extend = pc.ScriptType.extend;
script.attributes = new pc.ScriptAttributes(script);
pc.registerScript(script, name, app);
return script;
};
1 Like