|
1 | 1 | # Native Plugins |
2 | 2 |
|
3 | | -> Notice: Native plugin support for 3.6.2 is broken, please upgrade to 3.6.3 or later. |
| 3 | +> **Note**: |
| 4 | +> |
| 5 | +> * Please use the native plugin feature in CocosCreator 3.6.3 and above. |
| 6 | +> * The native plugin feature is not yet adapted for HarmonyOS related platforms. |
4 | 7 |
|
5 | | -Native Plugins are part of the editor plugins. By calling JSB interfaces (e.g. export with `sebind`) through native plugins, developers can extend the ability of JS scripts to call C++ interfaces, which can be very beneficial for solving script performance bottles and reusing existing codebases. As the engine is upgraded in the future, the native plugins will open up more features. |
| 8 | +Native plugins are part of the editor plugin system. Developers can use native plugins to call script binding interfaces (such as sebind) to extend the capability of JS scripts calling C++ interfaces, which is very beneficial for solving script performance bottlenecks and reusing existing codebases. |
6 | 9 |
|
7 | | -## Cooperation with existing *Editor Extensions* |
| 10 | +## Relationship with Existing Plugin System |
8 | 11 |
|
9 | | -Native plugins can exist independently of editor extensions, and users can use native plug-ins by copying them to the specified directory. |
| 12 | +Native plugins can exist independently of editor plugins. Users can use native plugins by copying them to specified directories. |
10 | 13 |
|
11 | | -At the same time, native plugins complement the existing editor extension system, extending the game's runtime capabilities. Developers use the ability of editor extensions to manage native plugins, such as download/switch/version upgrade, etc. |
| 14 | +At the same time, native plugins also serve as a supplement to the existing editor plugin system, extending the capabilities of the game runtime. They leverage the capabilities of editor plugins to manage native plugins, such as: download/on-off switching/version upgrade and other functions. |
| 15 | + |
| 16 | +### Plugin Structure |
| 17 | + |
| 18 | +Each plugin has a plugin description file `cc_plugin.json` in its root directory, which is a standard JSON file. |
| 19 | + |
| 20 | +When building native projects, the build system will recursively search for all `cc_plugin.json` files from the project's `extensions` and `native` directories to locate native plugins. Once `cc_plugins.json` is found in a directory, it will not search subdirectories further. |
12 | 21 |
|
13 | 22 | ## Installing Dependencies |
14 | 23 |
|
15 | | -To support plugin configuration parsing, a minimum of [NodeJS](https://nodejs.org/en/download/) version 8.0 is required. You can set the environment variable `PATH` or set `NODE_EXECUTABLE` to the full path to the node. In case CMake fails to locate NodeJS, you can directly set `NODE_EXECUTABLE` in CMakeLists.txt. This can now be done in `native/engine/common/localCfg.cmake`, starting from version 3.6.2, which is recommended. |
| 24 | +In a few environments where the editor is not installed, [NodeJS](https://nodejs.org/en/download/) version 8.0 or above needs to be installed to support plugin configuration parsing. Developers can set the NodeJS environment variable `PATH`, or specify it by setting `NODE_EXECUTABLE` in `CMakeLists.txt`. |
16 | 25 |
|
17 | | -## Example of the plugin's directory structure |
| 26 | +You can also set the environment variable `NODE_EXECUTABLE` to the full path of node. Starting from version 3.6.2, if CMake still cannot locate nodejs, you can directly set `NODE_EXECUTABLE` in `native/engine/common/localCfg.cmake`. |
| 27 | + |
| 28 | +## Basic Directory Structure Example |
18 | 29 |
|
19 | 30 | ``` |
20 | 31 | ├── cc_plugin.json |
21 | 32 | ├── android |
22 | | -│ ├── arm64-v8a |
23 | | -│ ├── armeabi-v7a |
24 | | -│ ├── x86 |
25 | | -│ └─ x86_64 |
26 | | -├─ ios |
27 | | -│ ├── include |
28 | | -│ └── lib |
29 | | -├─ mac |
30 | | -│ ├── include |
31 | | -│ └── lib |
32 | | -└─ windows |
33 | | - ├─ include |
34 | | - └── lib |
| 33 | +│ ├── arm64-v8a |
| 34 | +│ ├── armeabi-v7a |
| 35 | +│ ├── x86 |
| 36 | +│ └── x86_64 |
| 37 | +│__ google-play |
| 38 | +│ ├── arm64-v8a |
| 39 | +│ ├── armeabi-v7a |
| 40 | +│ ├── x86 |
| 41 | +│ └── x86_64 |
| 42 | +├── ios |
| 43 | +│ ├── include |
| 44 | +│ └── lib |
| 45 | +├── mac |
| 46 | +│ ├── include |
| 47 | +│ └── lib |
| 48 | +│── windows |
| 49 | +│ ├── include |
| 50 | +│ └── lib |
35 | 51 | ``` |
36 | 52 |
|
37 | | -The file `cc_plugin.json` provides the information necessary to load the plugin, and is the identifier of the native plugin. Each supported native platform corresponds to a directory that contains at least one `<PackageName>-Config.cmake` file. The build system will use CMake's [`find_package`](https://cmake.org/cmake/help/latest/command/find_package.html#id7) mechanism to locate or link to the required library files. |
| 53 | +The file `cc_plugin.json` provides the necessary information for loading plugins and serves as the identifier for native plugins. Each supported native platform corresponds to a directory, which contains at least one `<PackageName>-Config.cmake` file. The build system uses CMake's [`find_package`](https://cmake.org/cmake/help/latest/command/find_package.html#id7) mechanism to locate or link to the required library files. |
38 | 54 |
|
39 | | -If there are cross-platform source files or CMake configurations in the plugin, these files can be merged into the top-level directory. For example: [example project](https://github.com/PatriceJiang/ccplugin_tutorial/tree/main/NewProject/native/plugins/hello_cocos) |
| 55 | +If there are cross-platform source files or CMake configurations in the plugin, these files can be merged into the top-level directory. For details, please refer to the [example project](https://github.com/PatriceJiang/ccplugin_tutorial/tree/main/NewProject/native/plugins/hello_cocos). |
40 | 56 |
|
41 | | -## Description file `cc_plugin.json` format |
| 57 | +## Description File `cc_plugin.json` Format |
42 | 58 |
|
43 | 59 | ```ts |
44 | 60 | { |
45 | | - "name": string; // required: plugin name |
46 | | - "version": string; // required: plugin version |
47 | | - "engine-version":string; // mandatory: the range of the corresponding engine version |
48 | | - "author": string; // required: the author of the plugin |
49 | | - "description": string; // required: description of the plugin |
50 | | - "platforms":string[]; // optional: list of supported platforms, all native platforms are supported by default if not filled. Including windows, android, mac, ios |
51 | | - "disabled":true; // optional: Disable plugins on all platforms |
52 | | - "disable-by-platforms":string[]; // optional: Disable plugins for the specified platform |
53 | | - "modules": [{ // mandatory: the libraries included in the plugin, |
54 | | - "target":string; // mandatory: the name of the corresponding `find_package`, consistent with the first parameter of `CC_PLUGIN_ENTRY` |
55 | | - "depends": string|string[]; // optional: depends on other module names |
56 | | - "platforms":string[]; // optional: re-qualify the supported native platforms |
| 61 | + "name": string; // Required: Plugin name |
| 62 | + "version": string; // Required: Plugin version |
| 63 | + "engine-version":string; // Required: Corresponding engine version range |
| 64 | + "author": string; // Required: Plugin author |
| 65 | + "description": string; // Required: Plugin description |
| 66 | + "platforms":string[]; // Optional: Supported platform list, defaults to all native platforms if not specified. Includes windows, android, google-play, mac, ios |
| 67 | + "disabled":true; // Optional: Disable plugin |
| 68 | + "disable-by-platforms":string[]; // Optional: Disable plugin on specified platforms |
| 69 | + "modules": [{ // Required: Libraries included in the plugin |
| 70 | + "target":string; // Required: Corresponding `find_package` name, must be consistent with the first parameter of `CC_PLUGIN_ENTRY` |
| 71 | + "depends": string|string[]; // Optional: Dependencies on other module names |
| 72 | + "platforms":string[]; // Optional: Re-specify supported native platforms |
57 | 73 | }] |
| 74 | + |
58 | 75 | } |
59 | 76 | ``` |
60 | 77 |
|
61 | | -`engine-version` can specify a version range and exclude specific versions, for example: |
62 | | - |
| 78 | +`engine-version` can specify version ranges and exclude specific versions. Code example as follows: |
| 79 | + |
63 | 80 | ```ts |
64 | | -"engine-version": ">=3.3 <= 3.6.0 !3.5.2 || 4.x" |
| 81 | +"engine-version": ">=3.3 <= 3.6.0 !3.5.2|| 4.x" |
65 | 82 | ``` |
66 | 83 |
|
67 | | -### Example file |
68 | | - |
69 | | -Example of description file `cc_plugin.json` |
| 84 | +### File Example |
70 | 85 |
|
71 | 86 | ```json |
72 | 87 | { |
73 | | - "name": "hello-cocos-demo", |
74 | | - "version": "0.1.0", |
75 | | - "author": "demo group", |
76 | | - "engine-version":">=3.6.0", |
77 | | - "description": "demo project", |
| 88 | + "name":"hello-cocos-demo", |
| 89 | + "version":"1.0.0", |
| 90 | + "author":"cocos", |
| 91 | + "engine-version":">=3.6.3", |
| 92 | + "disabled":false, |
78 | 93 | "modules":[ |
79 | 94 | { |
80 | | - "target": "hello_cocos_glue" |
| 95 | + "target":"hello_cocos_glue" |
81 | 96 | } |
82 | 97 | ], |
83 | | - "platforms":[ "windows", "android", "mac", "iOS" ] |
| 98 | + "platforms":["windows", "android", "mac", "ios", "google-play"] |
84 | 99 | } |
85 | 100 | ``` |
| 101 | +## Creating Native Plugins |
86 | 102 |
|
87 | | -## Installing Native Plugins |
88 | | - |
89 | | -Developers can download and enable editor extensions from the Store that contain native plugins, and complete the installation of the plugins into the `extensions` directory. You can also get the plugin archive from the forums and manually extract it to the `native/` directory or its subdirectories. |
90 | | - |
91 | | -If you want to disable the plugin, or disable it only in a special platform, you can change the relevant fields(`disabled` and `disable-by-platforms`) in `cc_plugin.json`. |
92 | | - |
93 | | -> **Note**: Native plugins require CMake 3.12+, Android projects need to [specify cmake version](https://developer.android.com/studio/projects/install-ndk#vanilla_cmake) to 3.18.1. |
| 103 | +Cocos native projects use CMake for management. Native plugins are managed through the search paths/directories of find_package, so as long as the directory conforms to CMake find_package search rules, the plugin can be loaded correctly. Therefore, the development process of native plugins involves providing CMake configurations and related resources, as well as writing cc_plugin.json. For related examples, please refer to [Native Plugin Creation and Usage Example](./tutorial.md). |
94 | 104 |
|
95 | | -## Creating native plugins |
| 105 | +## Installing and Disabling Native Plugins |
96 | 106 |
|
97 | | -Cocos native projects are managed using CMake. Simply put, a native plugin is a search directory for `find_package` on specific platform. |
| 107 | +Download the created plugin package from [Native Plugin Creation and Usage Example](https://github.com/zhefengzhang/cocos-native-plugins), and choose to extract it to the `extensions` or `native` directory according to project needs. |
98 | 108 |
|
99 | | -As long as the directory matches the cmake find_package search rules, the plugin will load correctly, regardless of whether it is source code or precompiled libraries. Therefore, the development process of a native plugin is the process of providing the cmake configuration and related resources, and writing `cc_plugin.json`. |
| 109 | +If you want to disable the plugin, or disable it only on specific platforms, you can modify the `disabled` and `disable-by-platforms` fields in cc_plugin.json. |
100 | 110 |
|
101 | | -See [Cocos Native Plugin Quick Tutorial](./tutorial.md) to learn more about native plugins. |
| 111 | +> **Note**: Native plugins require CMake 3.12+. Android needs to [specify CMake version](https://developer.android.com/studio/projects/install-ndk#vanilla_cmake) as 3.18.1. Other platforms use the built-in CMake in the editor, so the version number does not need to be specified. |
0 commit comments