### Install Node Modules (NPM) Source: https://github.com/cocos/cocos-example-projects/blob/master/npm-case/README.md Installs the necessary Node.js modules listed in the project's package.json file. This is a standard step for setting up projects that depend on NPM packages. ```Shell npm install ``` -------------------------------- ### Including Cocos2d-x Engine CMakeLists CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/common/CMakeLists.txt Includes the main CMakeLists.txt file from the specified Cocos2d-x engine path (COCOS_X_PATH), integrating the engine's build setup into the project. ```CMake include(${COCOS_X_PATH}/CMakeLists.txt) ``` -------------------------------- ### Build Protobuf Files (NPM) Source: https://github.com/cocos/cocos-example-projects/blob/master/npm-case/README.md Runs a specific NPM script defined in package.json to regenerate protocol buffer files. This is required for the protobufjs demo included in the project. ```Shell npm run build-proto ``` -------------------------------- ### Include Cocos2d-x Engine CMakeLists.txt Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Includes the main `CMakeLists.txt` file from the Cocos2d-x engine directory specified by `COCOS_X_PATH`. This step integrates the engine's build system into the project. ```CMake include(${COCOS_X_PATH}/CMakeLists.txt) ``` -------------------------------- ### Setting Basic Build Configuration CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/common/CMakeLists.txt Configures the enabled languages (C, ASM), sets default values for development team, resource directory, and Cocos2d-x engine path, caching these values. ```CMake enable_language(C ASM) set(DEVELOPMENT_TEAM "" CACHE STRING "APPLE Developtment Team") set(RES_DIR "" CACHE STRING "Resource path") set(COCOS_X_PATH "" CACHE STRING "Path to engine/native/") ``` -------------------------------- ### Including Resource Configuration File CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/common/CMakeLists.txt Includes a CMake configuration file (cfg.cmake) located within the specified resource directory (RES_DIR), incorporating its settings into the current build process. ```CMake include(${RES_DIR}/proj/cfg.cmake) ``` -------------------------------- ### Setting Target OS Versions CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/common/CMakeLists.txt Specifies the minimum target versions for macOS (10.14) and iOS (11.0), forcing these values into the cache. ```CMake set(TARGET_OSX_VERSION "10.14" CACHE STRING "Target MacOSX version" FORCE) set(TARGET_IOS_VERSION "11.0" CACHE STRING "Target iOS version" FORCE) ``` -------------------------------- ### Setting C++ Standard CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/common/CMakeLists.txt Sets the C++ standard to C++17 for the project compilation. ```CMake set(CMAKE_CXX_STANDARD 17) ``` -------------------------------- ### Sending Requests to Native Layer (TypeScript) Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/README.md Provides example functions (`sayHelloBtn`, `changeLabelColorBtn`, `changeLightColorBtn`) intended to be triggered by UI button clicks. These functions use `jsb.bridge.sendToNative` to send specific method names or requests to the native platform, which is expected to process them and potentially trigger a callback back to the JS layer. ```TypeScript //Button click event for SAY HELLO public sayHelloBtn(){ jsb.bridge.sendToNative("requestLabelContent"); } //Button click event for CHANGE LABEL COLOR public changeLabelColorBtn(){ jsb.bridge.sendToNative("requestLabelColor"); } public changeLightColorBtn(){ jsb.bridge.sendToNative("requestBtnColor", "50"); } ``` -------------------------------- ### Configuring Cocos Project Build with CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/android/CMakeLists.txt This snippet sets up the basic CMake configuration for the project. It defines the minimum required CMake version, project name, library name, source directories, includes common build scripts, handles Android-specific build steps before and after the target, integrates an optional service module based on file existence, and finally defines the main shared library target. ```CMake cmake_minimum_required(VERSION 3.8) option(APP_NAME "Project Name" "CXJ") project(${APP_NAME} CXX) set(CC_LIB_NAME cocos) set(CC_PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(CC_PROJ_SOURCES) set(CC_COMMON_SOURCES) set(CC_ALL_SOURCES) include(${CMAKE_CURRENT_LIST_DIR}/../common/CMakeLists.txt) cc_android_before_target(${CC_LIB_NAME}) # -------------- SRART --------------- # USED BY COCOS SERVICE, DON'T REMOVE! if(EXISTS ${RES_DIR}/proj/service.cmake) set(SERVICE_NATIVE_DIR ${CMAKE_CURRENT_LIST_DIR}) include(${RES_DIR}/proj/service.cmake) endif() # -------------- END ---------------- add_library(${CC_LIB_NAME} SHARED ${CC_ALL_SOURCES}) cc_android_after_target(${CC_LIB_NAME}) ``` -------------------------------- ### Set C++ Standard in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Sets the C++ standard to C++17 for the project compilation, ensuring modern C++ features are available. ```CMake set(CMAKE_CXX_STANDARD 17) ``` -------------------------------- ### Configuring Cocos Project with CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/mac/CMakeLists.txt This CMake script configures a desktop project for Cocos, setting up project name, enabling sandbox, defining source and resource variables, including common CMake settings, and creating the executable target. It uses specific Cocos CMake functions for pre and post-target setup. ```CMake cmake_minimum_required(VERSION 3.8) set(APP_NAME "NewProject" CACHE STRING "Project Name") option(ENABLE_SANDBOX "enable sandbox entitlements.plist" ON) project(${APP_NAME} CXX) set(CMAKE_OSX_DEPLOYMENT_TARGET ${TARGET_OSX_VERSION}) set(CC_PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(CC_UI_RESOURCES) set(CC_PROJ_SOURCES) set(CC_ASSET_FILES) set(CC_COMMON_SOURCES) set(CC_ALL_SOURCES) include(${CC_PROJECT_DIR}/../common/CMakeLists.txt) set(EXECUTABLE_NAME ${APP_NAME}-desktop) cc_mac_before_target(${EXECUTABLE_NAME}) add_executable(${EXECUTABLE_NAME} ${CC_ALL_SOURCES}) cc_mac_after_target(${EXECUTABLE_NAME}) ``` -------------------------------- ### Include Resource Configuration File in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Includes a CMake configuration file named `cfg.cmake` located within the directory specified by the `RES_DIR` variable. This file likely contains resource-specific build settings. ```CMake include(${RES_DIR}/proj/cfg.cmake) ``` -------------------------------- ### Set Target OS Versions in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Specifies the minimum target operating system versions for macOS and iOS builds using CMake's `set` command with the `FORCE` option to override existing values. ```CMake set(TARGET_OSX_VERSION "10.14" CACHE STRING "Target MacOSX version" FORCE) set(TARGET_IOS_VERSION "11.0" CACHE STRING "Target iOS version" FORCE) ``` -------------------------------- ### Setting up Native Callback and Registering Methods (TypeScript) Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/README.md Implements a Cocos Creator component (`CallNative`) that initializes the `MethodManager` and assigns a handler to `jsb.bridge.onNative`. This handler calls `MethodManager.instance.applyMethod` with the received method name and arguments. It also registers specific methods (`changeLabelContent`, `changeLabelColor`, `changeLightColor`) that modify UI elements with the `MethodManager`. ```TypeScript @ccclass('CallNative') export class CallNative extends Component { //static eventMap: Map = new Map(); @property(Label) public labelForContent : Label|undefined; @property(Label) public labelForColor : Label|undefined; @property(Label) public labelForSize : Label|undefined; start() { new MethodManager; jsb.bridge.onNative = (methodName: string, arg1?: string | null) => { console.log("Trigger event for " + methodName + " is " + MethodManager.instance.applyMethod(methodName, arg1!)); }; this.registerAllScriptEvent(); } public registerAllScriptEvent() { MethodManager.instance.addMethod("changeLabelContent", (usr: string) => { this.changeLabelContent(usr); }); MethodManager.instance.addMethod("changeLabelColor", () => { this.changeLabelColor(); }); MethodManager.instance.addMethod("changeLightColor", () => { this.changeLightColor(); }); } //Methods to apply public changeLabelContent(user: string): void { CC_LOG_DEBUG("Hello " + user + " I'm K"); this.labelForContent!.string = "Hello " + user + " ! I'm K"; } public changeLabelColor(): void { this.labelForColor!.color = new Color("#B8F768"); this.labelForContent!.color = new Color("#87E9B2"); } public changeLightColor(): void { this.lightToChange!.color = new Color("#90FF03"); } } ``` -------------------------------- ### Define Project Feature Options in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Configures various build options using CMake's `option` command. These options enable or disable specific features and components like debug modes, JavaScript engines (V8, JSC), networking (WebSocket, SocketIO), audio, UI elements (EditBox), media players (Video, WebView), middleware, physics, and rendering features. ```CMake option(CC_DEBUG_FORCE "Force enable CC_DEBUG in release mode" OFF) option(USE_SE_V8 "Use V8 JavaScript Engine" ON) option(USE_V8_DEBUGGER "Compile v8 inspector ws server" ON) option(USE_V8_DEBUGGER_FORCE "Force enable debugger in release mode" OFF) option(USE_SOCKET "Enable WebSocket & SocketIO" ON) option(USE_AUDIO "Enable Audio" ON) #Enable AudioEngine option(USE_EDIT_BOX "Enable EditBox" ON) option(USE_SE_JSC "Use JavaScriptCore on MacOSX/iOS" OFF) option(USE_VIDEO "Enable VideoPlayer Component" ON) option(USE_WEBVIEW "Enable WebView Component" ON) option(USE_MIDDLEWARE "Enable Middleware" ON) option(USE_DRAGONBONES "Enable Dragonbones" ON) option(USE_SPINE "Enable Spine" ON) option(USE_WEBSOCKET_SERVER "Enable WebSocket Server" OFF) option(USE_JOB_SYSTEM_TASKFLOW "Use taskflow as job system backend" OFF) option(USE_JOB_SYSTEM_TBB "Use tbb as job system backend" OFF) option(USE_PHYSICS_PHYSX "USE PhysX Physics" ON) option(USE_DEBUG_RENDERER "USE Debug Renderer" ON) option(USE_GEOMETRY_RENDERER "USE Geometry Renderer" ON) option(USE_WEBP "USE Webp" ON) ``` -------------------------------- ### Validate Resource Directory (RES_DIR) in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Checks if the `RES_DIR` variable has been set. If it is empty, a fatal error message is displayed, halting the CMake configuration process. ```CMake if(NOT RES_DIR) message(FATAL_ERROR "RES_DIR is not set!") endif() ``` -------------------------------- ### Defining Build Feature Options CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/common/CMakeLists.txt Defines various build options to enable or disable specific features and components within the project, such as JavaScript engines, networking, audio, video, UI elements, middleware, physics, and rendering features. ```CMake option(CC_DEBUG_FORCE "Force enable CC_DEBUG in release mode" OFF) option(USE_SE_V8 "Use V8 JavaScript Engine" ON) option(USE_V8_DEBUGGER "Compile v8 inspector ws server" ON) option(USE_V8_DEBUGGER_FORCE "Force enable debugger in release mode" OFF) option(USE_SOCKET "Enable WebSocket & SocketIO" ON) option(USE_AUDIO "Enable Audio" ON) #Enable AudioEngine option(USE_EDIT_BOX "Enable EditBox" ON) option(USE_SE_JSC "Use JavaScriptCore on MacOSX/iOS" OFF) option(USE_VIDEO "Enable VideoPlayer Component" ON) option(USE_WEBVIEW "Enable WebView Component" ON) option(USE_MIDDLEWARE "Enable Middleware" ON) option(USE_DRAGONBONES "Enable Dragonbones" ON) option(USE_SPINE "Enable Spine" ON) option(USE_WEBSOCKET_SERVER "Enable WebSocket Server" OFF) option(USE_JOB_SYSTEM_TASKFLOW "Use taskflow as job system backend" OFF) option(USE_JOB_SYSTEM_TBB "Use tbb as job system backend" OFF) option(USE_PHYSICS_PHYSX "Use PhysX Physics" ON) option(USE_OCCLUSION_QUERY "Use Occlusion Query" ON) option(USE_DEBUG_RENDERER "Use Debug Renderer" ON) option(USE_GEOMETRY_RENDERER "Use Geometry Renderer" ON) option(USE_WEBP "Use Webp" ON) ``` -------------------------------- ### Enable Languages (C, ASM) in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Enables the C and ASM languages for compilation within the CMake project, which are often required for native code or specific engine components. ```CMake enable_language(C ASM) ``` -------------------------------- ### Validate Cocos2d-x Engine Path (COCOS_X_PATH) in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Checks if the `COCOS_X_PATH` variable, which points to the engine's native directory, has been set. If it is empty, a fatal error message is displayed. ```CMake if(NOT COCOS_X_PATH) message(FATAL_ERROR "COCOS_X_PATH is not set!") endif() ``` -------------------------------- ### Configuring Cocos Project Build with CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/android/CMakeLists.txt This snippet defines the CMake build configuration for a Cocos project. It sets the minimum required CMake version, defines the project name and variables for library name and source directories, includes common CMake settings from another file, and declares a shared library target named 'cocos' using the collected source files. ```CMake cmake_minimum_required(VERSION 3.8) option(APP_NAME "Project Name" "NewProject") project(${APP_NAME} CXX) set(CC_LIB_NAME cocos) set(CC_PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(CC_PROJ_SOURCES) set(CC_COMMON_SOURCES) set(CC_ALL_SOURCES) include(${CMAKE_CURRENT_LIST_DIR}/../common/CMakeLists.txt) cc_android_before_target(${CC_LIB_NAME}) add_library(${CC_LIB_NAME} SHARED ${CC_ALL_SOURCES}) cc_android_after_target(${CC_LIB_NAME}) ``` -------------------------------- ### Validating Resource Directory Path CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/common/CMakeLists.txt Checks if the RES_DIR variable is set. If not, it prints a fatal error message and stops the CMake configuration process, indicating that the resource path is required. ```CMake if(NOT RES_DIR) message(FATAL_ERROR "RES_DIR is not set!") endif() ``` -------------------------------- ### Set Core Project Variables in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Defines essential project variables using CMake's `set` command. These variables include the Apple Development Team ID, the path to project resources, and the path to the Cocos2d-x engine's native directory. ```CMake set(DEVELOPMENT_TEAM "" CACHE STRING "APPLE Developtment Team") set(RES_DIR "" CACHE STRING "Resource path") set(COCOS_X_PATH "" CACHE STRING "Path to engine/native/") ``` -------------------------------- ### Append Common Source Files in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/common/CMakeLists.txt Adds the main game source files, `Game.h` and `Game.cpp`, located in the `Classes` subdirectory relative to the current CMake file, to the `CC_COMMON_SOURCES` list. ```CMake list(APPEND CC_COMMON_SOURCES ${CMAKE_CURRENT_LIST_DIR}/Classes/Game.h ${CMAKE_CURRENT_LIST_DIR}/Classes/Game.cpp ) ``` -------------------------------- ### Adding Common Source Files CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/common/CMakeLists.txt Appends the specified header (Game.h) and source (Game.cpp) files from the current directory's Classes subdirectory to the CC_COMMON_SOURCES list, which is likely used later to define project targets. ```CMake list(APPEND CC_COMMON_SOURCES ${CMAKE_CURRENT_LIST_DIR}/Classes/Game.h ${CMAKE_CURRENT_LIST_DIR}/Classes/Game.cpp ) ``` -------------------------------- ### Defining Method Manager Class (TypeScript) Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/README.md Defines a utility class `MethodManager` to store and manage JavaScript functions using a Map. It provides methods to add, apply (call), and remove functions by a string name. This allows a single native-to-JS callback (`jsb.bridge.onNative`) to trigger different actions based on the method name passed from native. ```TypeScript export class MethodManager { private methodMap: Map; public static instance: MethodManager = new MethodManager; public addMethod(methodName: String, f: Function): boolean { if (!this.methodMap.get(methodName)) { this.methodMap.set(methodName, f); return true; } return false; } public applyMethod(methodName: String, arg?: String): boolean { if (!this.methodMap.get(methodName)) { console.log("Function not exist"); return false; } var f = this.methodMap.get(methodName); try { f?.call(null, arg); return true; } catch (e) { console.log("Function trigger error: " + e); return false; } } public removeMethod(methodName: String):any{ return this.methodMap.delete(methodName); } constructor() { this.methodMap = new Map(); MethodManager.instance = this; } } ``` -------------------------------- ### Validating Cocos2d-x Engine Path CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/common/CMakeLists.txt Checks if the COCOS_X_PATH variable is set. If not, it prints a fatal error message and stops the CMake configuration process, indicating that the path to the Cocos2d-x engine is required. ```CMake if(NOT COCOS_X_PATH) message(FATAL_ERROR "COCOS_X_PATH is not set!") endif() ``` -------------------------------- ### Linking Libraries to Target in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/plugins/hello_cocos/src/CMakeLists.txt Links the 'hello_cocos_glue' target library to its required dependencies. It links against 'hello_cocos' and another library specified by the '${ENGINE_NAME}' variable (comment indicates 'cocos_engine'). ```CMake target_link_libraries(hello_cocos_glue hello_cocos ${ENGINE_NAME} # cocos_engine ) ``` -------------------------------- ### Configuring Cocos Project with CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/win64/CMakeLists.txt This snippet defines the project name, sets up variables for source files, includes a common CMake configuration file, and declares the executable target for the project. ```CMake cmake_minimum_required(VERSION 3.8) set(APP_NAME "native_plugin_demo" CACHE STRING "Project Name") project(${APP_NAME} CXX) set(CC_PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(CC_UI_RESOURCES) set(CC_PROJ_SOURCES) set(CC_COMMON_SOURCES) set(CC_ALL_SOURCES) include(${CC_PROJECT_DIR}/../common/CMakeLists.txt) set(EXECUTABLE_NAME ${APP_NAME}) cc_windows_before_target(${EXECUTABLE_NAME}) add_executable(${EXECUTABLE_NAME} ${CC_ALL_SOURCES} ) cc_windows_after_target(${EXECUTABLE_NAME}) ``` -------------------------------- ### Adding Include Directories in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/plugins/hello_cocos/src/CMakeLists.txt Specifies private include directories for the 'hello_cocos_glue' target. This allows the compiler to find necessary header files located two directories up from the source directory. ```CMake target_include_directories(hello_cocos_glue PRIVATE ${_HELLO_COCOS_GLUE_SRC_DIR}/../include ) ``` -------------------------------- ### Adding Static Library in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/plugins/hello_cocos/src/CMakeLists.txt Defines a static library target named 'hello_cocos_glue'. It specifies the source file required to build this library, using the previously defined source directory variable. ```CMake add_library(hello_cocos_glue STATIC ${_HELLO_COCOS_GLUE_SRC_DIR}/hello_cocos-glue.cpp) ``` -------------------------------- ### Configuring Cocos Desktop Project with CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/mac/CMakeLists.txt This snippet defines the project name, enables optional features, sets platform-specific variables, includes common sources, lists project-specific sources, and defines the executable target for a macOS build of a Cocos application using CMake. ```CMake cmake_minimum_required(VERSION 3.8) set(APP_NAME "CXJ" CACHE STRING "Project Name") option(ENABLE_SANDBOX "enable sandbox entitlements.plist" ON) project(${APP_NAME} CXX) set(CMAKE_OSX_DEPLOYMENT_TARGET ${TARGET_OSX_VERSION}) set(CC_PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(CC_UI_RESOURCES) set(CC_PROJ_SOURCES) set(CC_ASSET_FILES) set(CC_COMMON_SOURCES) set(CC_ALL_SOURCES) include(${CC_PROJECT_DIR}/../common/CMakeLists.txt) set(EXECUTABLE_NAME ${APP_NAME}-desktop) set(JSB_BRIDGE_TEST_SOURCE ${CMAKE_CURRENT_LIST_DIR}/JsbBridgeTest.h ${CMAKE_CURRENT_LIST_DIR}/JsbBridgeTest.mm ${CMAKE_CURRENT_LIST_DIR}/MyMacPlatform.h ${CMAKE_CURRENT_LIST_DIR}/MyMacPlatform.mm ${CMAKE_} ) list(APPEND CC_PROJ_SOURCES ${JSB_BRIDGE_TEST_SOURCE} ) cc_mac_before_target(${EXECUTABLE_NAME}) add_executable(${EXECUTABLE_NAME} ${CC_ALL_SOURCES}) cc_mac_after_target(${EXECUTABLE_NAME}) ``` -------------------------------- ### Configuring Cocos2d-x iOS Project Build - CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/native/engine/ios/CMakeLists.txt This CMake script sets up the build configuration for an iOS application using Cocos2d-x. It defines the project name, specifies source file lists for the project and common components, includes a common CMake configuration file, and defines the main executable target for the iOS platform. ```CMake cmake_minimum_required(VERSION 3.8) set(CMAKE_SYSTEM_NAME iOS) set(APP_NAME "CXJ" CACHE STRING "Project Name") project(${APP_NAME} CXX) set(CC_PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(CC_UI_RESOURCES) set(CC_PROJ_SOURCES) set(CC_ASSET_FILES) set(CC_COMMON_SOURCES) set(CC_ALL_SOURCES) include(${CC_PROJECT_DIR}/../common/CMakeLists.txt) set(EXECUTABLE_NAME ${APP_NAME}-mobile) set(JSB_BRIDGE_TEST_SOURCE ${CMAKE_CURRENT_LIST_DIR}/JsbBridgeTest.h ${CMAKE_CURRENT_LIST_DIR}/JsbBridgeTest.mm ${CMAKE_CURRENT_LIST_DIR}/MyIOSPlatform.h ${CMAKE_CURRENT_LIST_DIR}/MyIOSPlatform.mm ${CMAKE_} ) list(APPEND CC_PROJ_SOURCES ${JSB_BRIDGE_TEST_SOURCE} ) cc_ios_before_target(${EXECUTABLE_NAME}) add_executable(${EXECUTABLE_NAME} ${CC_ALL_SOURCES}) cc_ios_after_target(${EXECUTABLE_NAME}) ``` -------------------------------- ### Configuring Cocos2d-x iOS Project with CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/engine/ios/CMakeLists.txt This CMake script sets the minimum required CMake version, specifies iOS as the target system, defines the application name, sets up variables for project directories and source/resource files, includes common CMake logic from a shared file, defines the executable name, and adds the executable target with platform-specific pre and post-target hooks. ```CMake cmake_minimum_required(VERSION 3.8) set(CMAKE_SYSTEM_NAME iOS) set(APP_NAME "NewProject" CACHE STRING "Project Name") project(${APP_NAME} CXX) set(CC_PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(CC_UI_RESOURCES) set(CC_PROJ_SOURCES) set(CC_ASSET_FILES) set(CC_COMMON_SOURCES) set(CC_ALL_SOURCES) include(${CC_PROJECT_DIR}/../common/CMakeLists.txt) set(EXECUTABLE_NAME ${APP_NAME}-mobile) cc_ios_before_target(${EXECUTABLE_NAME}) add_executable(${EXECUTABLE_NAME} ${CC_ALL_SOURCES}) cc_ios_after_target(${EXECUTABLE_NAME}) ``` -------------------------------- ### Setting Source Directory in CMake Source: https://github.com/cocos/cocos-example-projects/blob/master/native-plugin/native/plugins/hello_cocos/src/CMakeLists.txt Sets a variable '_HELLO_COCOS_GLUE_SRC_DIR' to the current directory where the CMakeLists.txt file is located. This variable is used to reference source files relative to the script's location. ```CMake set(_HELLO_COCOS_GLUE_SRC_DIR ${CMAKE_CURRENT_LIST_DIR}) ``` -------------------------------- ### Define JsbBridgeWrapper Interface in Objective-C Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/README-jsbBridgeWrapper.md Presents the Objective-C interface for JsbBridgeWrapper, showing the native-side methods available for managing script event listeners and dispatching events to the script layer. It includes methods for adding, removing, and dispatching events, mirroring the JavaScript API. ```Objective-C //In Objective-C typedef void (^OnScriptEventListener)(NSString*); @interface JsbBridgeWrapper : NSObject /** * Get the instance of JsbBridgetWrapper */ + (instancetype)sharedInstance; /** * Add a listener to specified event, if the event does not exist, the wrapper will create one. Concurrent listener will be ignored */ - (void)addScriptEventListener:(NSString*)eventName listener:(OnScriptEventListener)listener; /** * Remove listener for specified event, concurrent event will be deleted. Return false only if the event does not exist */ - (bool)removeScriptEventListener:(NSString*)eventName listener:(OnScriptEventListener)listener; /** * Remove all listener for event specified. */ - (void)removeAllListenersForEvent:(NSString*)eventName; /** * Remove all event registered. Use it carefully! */ - (void)removeAllListeners; /** * Dispatch the event with argument, the event should be registered in javascript, or other script language in future. */ - (void)dispatchEventToScript:(NSString*)eventName arg:(NSString*)arg; /** * Dispatch the event which is registered in javascript, or other script language in future. */ - (void)dispatchEventToScript:(NSString*)eventName; @end ``` -------------------------------- ### Define JsbBridgeWrapper Class in Java Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/README-jsbBridgeWrapper.md Shows the Java class definition for JsbBridgeWrapper, outlining the native-side methods for handling script event listeners and dispatching events back to the script layer. It provides methods for adding, removing, and dispatching events, analogous to the JavaScript and Objective-C APIs. ```Java //In JAVA public class JsbBridgeWrapper { public interface OnScriptEventListener { void onScriptEvent(String arg); } /** * Add a listener to specified event, if the event does not exist, the wrapper will create one. Concurrent listener will be ignored */ public void addScriptEventListener(String eventName, OnScriptEventListener listener); /** * Remove listener for specified event, concurrent event will be deleted. Return false only if the event does not exist */ public boolean removeScriptEventListener(String eventName, OnScriptEventListener listener); /** * Remove all listener for event specified. */ public void removeAllListenersForEvent(String eventName); /** * Remove all event registered. Use it carefully! */ public void removeAllListeners() { this.eventMap.clear(); } /** * Dispatch the event with argument, the event should be registered in javascript, or other script language in future. */ public void dispatchEventToScript(String eventName, String arg); /** * Dispatch the event which is registered in javascript, or other script language in future. */ public void dispatchEventToScript(String eventName); } ``` -------------------------------- ### Define JsbBridgeWrapper Interface in TypeScript Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/README-jsbBridgeWrapper.md Describes the TypeScript/JavaScript definition of the JsbBridgeWrapper interface and namespace, detailing the types and functions available for interacting with native code via the bridge. It includes methods for adding, removing, and dispatching events. ```TypeScript /** * Listener for jsbBridgeWrapper's event. * It takes one argument as string which is transferred by jsbBridge. */ export type OnNativeEventListener = (arg: string) => void; export namespace jsbBridgeWrapper { /** If there's no event registered, the wrapper will create one */ export function addNativeEventListener(eventName: string, listener: OnNativeEventListener); /** * Dispatch the event registered on Objective-C, Java etc. * No return value in JS to tell you if it works. */ export function dispatchEventToNative(eventName: string, arg?: string); /** * Remove all listeners relative. */ export function removeAllListenersForEvent(eventName: string); /** * Remove the listener specified */ export function removeNativeEventListener(eventName: string, listener: OnNativeEventListener); /** * Remove all events, use it carefully! */ export function removeAllListeners(); } ``` -------------------------------- ### Add Native Event Listener in JavaScript Source: https://github.com/cocos/cocos-example-projects/blob/master/native-script-bridge/README-jsbBridgeWrapper.md Demonstrates how to register a listener for a specific native event ("A") using jsb.jsbBridgeWrapper.addNativeEventListener. It shows using an anonymous function to correctly handle the this context when calling a class method upon event trigger. ```JavaScript //When A is triggered, this.A will be applied jsb.jsbBridgeWrapper.addNativeEventListener("A", (usr: string) => { this.A(usr); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.