diff --git a/documentation/docs/api/logging-hooks.md b/documentation/docs/api/logging-hooks.md new file mode 100644 index 0000000..eada9c8 --- /dev/null +++ b/documentation/docs/api/logging-hooks.md @@ -0,0 +1,42 @@ +# Logging Hooks + +Intercept logs from the Unity module. + +## Type Definition + +```ts title="Type Definition" +type UnityConfig = { + readonly print?: (message: string) => void; + readonly printErr?: (message: string) => void; +}; +``` + +## Implementation + +The `print` and `printErr` functions supplied in the `UnityConfig` will be passed verbatim to `createUnityInstance`. These functions will receive log messages coming from the Unity module - in particular, logs that would normally be printed to `stdout` or `stderr`. + +:::warning +The messages received by these functions are distinct from the ones that are already printed to the console by default via `UnityEngine.Debug.Log` etc. +::: + +## Example Usage + +Below is an example of using `print` and `printErr` to print module logs to the console. + +```jsx {10-11} showLineNumbers title="App.jsx" +import React from "react"; +import { Unity, useUnityContext } from "react-unity-webgl"; + +function App() { + const { unityProvider } = useUnityContext({ + loaderUrl: "build/myunityapp.loader.js", + dataUrl: "build/myunityapp.data", + frameworkUrl: "build/myunityapp.framework.js", + codeUrl: "build/myunityapp.wasm", + print: console.log, + printErr: console.error, + }); + + return ; +} +``` diff --git a/documentation/sidebars.json b/documentation/sidebars.json index c2e18be..a14a112 100644 --- a/documentation/sidebars.json +++ b/documentation/sidebars.json @@ -191,6 +191,11 @@ "type": "doc", "id": "api/canvas-id", "label": "Custom Canvas ID" + }, + { + "type": "doc", + "id": "api/logging-hooks", + "label": "Logging Hooks" } ] } diff --git a/module/source/hooks/use-unity-arguments.ts b/module/source/hooks/use-unity-arguments.ts index 026d7bb..c7911a4 100644 --- a/module/source/hooks/use-unity-arguments.ts +++ b/module/source/hooks/use-unity-arguments.ts @@ -66,27 +66,11 @@ const useUnityArguments = (unityProps: UnityProps): UnityArguments => { // Assigns the print hook to the Unity arguments object. This hook will // be called whenever the Unity instance prints a message. - print: - /** - * Intercept print events in order to catch messages and send them to - * the unity context instead. - * @param message The message to be printed. - */ - (message: string) => { - // TODO -- Re-implement this hook. - }, + print: unityProps.unityProvider.unityConfig.print, // Assigns the print error hook to the Unity arguments object. This hook // will be called whenever the Unity instance prints an error. - printErr: - /** - * Intercept print error events in order to catch messages and send them - * to the unity context instead. - * @param error The error to be printed. - */ - (error: string) => { - // TODO -- Re-implement this hook. - }, + printErr: unityProps.unityProvider.unityConfig.printErr, }), [] ); diff --git a/module/source/types/unity-config.ts b/module/source/types/unity-config.ts index 061a10a..9916de4 100644 --- a/module/source/types/unity-config.ts +++ b/module/source/types/unity-config.ts @@ -18,6 +18,8 @@ type ConfigurableUnityArguments = Pick< | "productVersion" | "webglContextAttributes" | "cacheControl" + | "print" + | "printErr" >; /**