react-unity-webgl
Version:
React Unity WebGL provides a modern solution for embedding Unity WebGL builds in your React Application while providing advanced APIs for two way communication and interaction between Unity and React.
46 lines (45 loc) • 2.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useUnityMetricsInfo = void 0;
var react_1 = require("react");
/**
* Hook to retrieve metrics information from the Unity instance.
* This is a placeholder implementation and should be replaced with actual logic
* to interact with the Unity instance and retrieve metrics.
* @param unityProvider the Unity provider instance
* @param metricsConfig configuration for metrics retrieval
* @returns
*/
var useUnityMetricsInfo = function (getMetricsInfo, metricsConfig) {
// Initial state for metrics info
var _a = (0, react_1.useState)({}), metricsInfo = _a[0], setMetricsInfo = _a[1];
// Effect to periodically retrieve metrics info from the Unity instance
// This assumes that the Unity provider has a method `getMetricsInfo` to retrieve the metrics.
// If this method does not exist, you will need to implement it in your Unity instance.
// The interval for retrieving metrics is configurable via `metricsConfig`.
(0, react_1.useEffect)(function () {
// Set up an interval to retrieve metrics info from the Unity instance
// The interval is defined by `metricsConfig.interval`, defaulting to 1000ms
// if not provided.
var intervalId = setInterval(function () {
var info = getMetricsInfo();
if (typeof info === "undefined") {
// If the info is undefined, return early.
// This could happen if the Unity instance is not ready or if there is an error
// retrieving the metrics.
return;
}
// Update the state with the retrieved metrics info
// This will trigger a re-render of the component using this hook
// with the new metrics info.
setMetricsInfo(info);
}, metricsConfig.interval || 1000);
return function () {
// Clear the interval when the component unmounts or when the dependencies change
// to prevent memory leaks and unnecessary calls.
clearInterval(intervalId);
};
}, [getMetricsInfo, metricsConfig.interval]);
return metricsInfo;
};
exports.useUnityMetricsInfo = useUnityMetricsInfo;