Cocos creator editor extensions

I try to create a script and prefab from my custom extension and attach that script to created prefab but it keep saying that my create class is not child class of Component

const scriptResult = await Editor.Message.request(
        "asset-db",
        "create-asset",
        "db://assets/" + name + ".ts",
        "import { _decorator, Component, Node } from 'cc';\nconst { ccclass, property } = _decorator;\n\n@ccclass('" +
          name +
          "')\nexport class " +
          name +
          " extends Component {\n    start() {\n\n    }\n\n    update(deltaTime: number) {\n        \n    }\n}\n\n",
        {}
      );

Here is how i create prefab

const prefab = await Editor.Message.request(
        "asset-db",
        "create-asset",
        "db://assets/" + name + ".prefab",
        '[\r\n  {\r\n    "__type__": "cc.Prefab",\r\n    "_name": "",\r\n    "_objFlags": 0,\r\n    "_native": "",\r\n    "data": {\r\n      "__id__": 1\r\n    },\r\n    "optimizationPolicy": 0,\r\n    "asyncLoadAssets": false\r\n  },\r\n  {\r\n    "__type__": "cc.Node",\r\n    "_name": "Node",\r\n    "_objFlags": 0,\r\n    "_parent": null,\r\n    "_children": [],\r\n    "_active": true,\r\n    "_components": [],\r\n    "_prefab": {\r\n      "__id__": 2\r\n    },\r\n    "_lpos": {\r\n      "__type__": "cc.Vec3",\r\n      "x": 0,\r\n      "y": 0,\r\n      "z": 0\r\n    },\r\n    "_lrot": {\r\n      "__type__": "cc.Quat",\r\n      "x": 0,\r\n      "y": 0,\r\n      "z": 0,\r\n      "w": 1\r\n    },\r\n    "_lscale": {\r\n      "__type__": "cc.Vec3",\r\n      "x": 1,\r\n      "y": 1,\r\n      "z": 1\r\n    },\r\n    "_layer": 1073741824,\r\n    "_euler": {\r\n      "__type__": "cc.Vec3",\r\n      "x": 0,\r\n      "y": 0,\r\n      "z": 0\r\n    },\r\n    "_id": ""\r\n  },\r\n  {\r\n    "__type__": "cc.PrefabInfo",\r\n    "root": {\r\n      "__id__": 1\r\n    },\r\n    "asset": {\r\n      "__id__": 0\r\n    },\r\n    "fileId": "c46/YsCPVOJYA4mWEpNYRx"\r\n  }\r\n]',
        {}
      );
 [Scene] ctor with name aa30f0c6-348d-4bbd-bcf7-fcbd80c78329 is not child class of Component```

We’ll check the problem

1 Like

I modified your code and now it doesn’t report errors. I solved the problem by comparing the full prefab file.

59467

async createScriptAndPrefab() {
    var name = "Test";
    const scriptResult = await Editor.Message.request(
      "asset-db",
      "create-asset",
      "db://assets/" + name + ".ts",
      "import { _decorator, Component, Node } from 'cc';\nconst { ccclass, property } = _decorator;\n\n@ccclass('" +
      name +
      "')\nexport class " +
      name +
      " extends Component {\n    start() {\n\n    }\n\n    update(deltaTime: number) {\n        \n    }\n}\n\n",
    );
    var uuid = scriptResult!.uuid;
    var compressUuid = Editor.Utils.UUID.compressUUID(uuid, false);
    const prefab = await Editor.Message.request(
      "asset-db",
      "create-asset",
      "db://assets/" + name + ".prefab",
      `[\n  {\n    "__type__": "cc.Prefab",\n    "_name": "${name}",\n    "_objFlags": 0, \n "__editorExtras__": {},  \n    "_native": "",\n    "data": {\n      "__id__": 1\n    },\n    "optimizationPolicy": 0,\n  "persistent": false\n  },\n  
      {\n   "__type__": "cc.Node",\n    "_name": "Node",\n    "_objFlags": 0,\n "__editorExtras__": {}, \n  "_parent": null,\n    "_children": [],\n    "_active": true,\n    "_components": [ \n{ \n      "__id__": 2  \n    } \n ],\n  
      "_prefab": {\n      "__id__": 4\n    },\n    "_lpos": {\n      "__type__": "cc.Vec3",\n      "x": 0,\n      "y": 0,\n      "z": 0\n    },\n    "_lrot": {\n      "__type__": "cc.Quat",\n      "x": 0,\n      "y": 0,\n      "z": 0,\n      "w": 1\n    },\n    "_lscale": {\n      "__type__": "cc.Vec3",\n      "x": 1,\n      "y": 1,\n      "z": 1\n    },\n "_mobility": 0, \n    "_layer": 1073741824,\n    "_euler": {\n      "__type__": "cc.Vec3",\n      "x": 0,\n      "y": 0,\n      "z": 0\n    },\n    "_id": ""\n  },\n  
      {\n   "__type__": "${compressUuid}", \n "_name": "", \n "_objFlags": 0, \n "__editorExtras__": {}, \n"node": { \n "__id__": 1 \n }, \n "_enabled": true, \n "__prefab": { \n "__id__": 3 \n }, \n "_id": "" \n }, \n
      {\n   "__type__": "cc.CompPrefabInfo", \n    "fileId": "a823Am7hZKbazCoTnG8r+Q" \n }, \n
      {\n    "__type__": "cc.PrefabInfo",\n    "root": {\n      "__id__": 1\n    },\n    "asset": {\n      "__id__": 0\n    },\n    "fileId": "59D3HVHkBGKq02vMeUD0G+", \n "instance": null, \n "targetOverrides": null \n}\n]`,
    );
  }

Test Project:
blank-template.zip (737.0 KB)

Before using the plug-in, you need to execute npm install

1 Like

Thank you for your quickly support, if I generate prefab follow your code, it’s work. But if i have complicate prefab and I just wanna copy it, then attach auto generated script file then are there any way i can proceed it?

I’m sure you already know how to create scripts and prefabs, even for complex prefabs you just need to modify the plugin script according to the contents of the.prefab file.

In addition, if you want to copy Prefab, you can use copy-asset

1 Like