Skip to content

Make print and printErr functions configurable #549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions documentation/docs/api/logging-hooks.md
Original file line number Diff line number Diff line change
@@ -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 <Unity unityProvider={unityProvider} />;
}
```
5 changes: 5 additions & 0 deletions documentation/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@
"type": "doc",
"id": "api/canvas-id",
"label": "Custom Canvas ID"
},
{
"type": "doc",
"id": "api/logging-hooks",
"label": "Logging Hooks"
}
]
}
Expand Down
20 changes: 2 additions & 18 deletions module/source/hooks/use-unity-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
[]
);
Expand Down
2 changes: 2 additions & 0 deletions module/source/types/unity-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type ConfigurableUnityArguments = Pick<
| "productVersion"
| "webglContextAttributes"
| "cacheControl"
| "print"
| "printErr"
>;

/**
Expand Down