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.
97 lines (96 loc) • 5.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useUnityContext = void 0;
var react_1 = require("react");
var use_event_system_1 = require("./use-event-system");
/**
* Custom hook to create a Unity context.
* This hook initializes the Unity instance and provides the necessary state and methods.
* @param unityConfig - Configuration object for the Unity instance.
* @returns An object containing the Unity context state and methods.
*/
var useUnityContext = function (unityConfig) {
var _a = (0, react_1.useState)(null), unityInstance = _a[0], setUnityInstance = _a[1];
var _b = (0, react_1.useState)(0), loadingProgression = _b[0], setLoadingProgression = _b[1];
var _c = (0, react_1.useState)(false), isLoaded = _c[0], setIsLoaded = _c[1];
var _d = (0, react_1.useState)(), initialisationError = _d[0], setInitialisationError = _d[1];
// Use the event system hook to manage external React Unity events.
// This hook provides methods to add and remove event listeners for Unity events.
// It returns an object with the methods addEventListener and removeEventListener.
var eventSystem = (0, use_event_system_1.useEventSystem)();
// Create a ref to hold the UnityProvider instance, it consists of a selection
// of properties from the UnityConfig and methods to interact with the Unity instance.
// This allows us to avoid unnecessary re-renders when the UnityProvider is updated.
// The useRef hook is used to persist the UnityProvider instance across renders.
var unityProvider = (0, react_1.useRef)({
companyName: unityConfig.companyName,
productName: unityConfig.productName,
productVersion: unityConfig.productVersion,
codeUrl: unityConfig.codeUrl,
dataUrl: unityConfig.dataUrl,
frameworkUrl: unityConfig.frameworkUrl,
loaderUrl: unityConfig.loaderUrl,
memoryUrl: unityConfig.memoryUrl,
symbolsUrl: unityConfig.symbolsUrl,
streamingAssetsUrl: unityConfig.streamingAssetsUrl,
workerUrl: unityConfig.workerUrl,
webglContextAttributes: unityConfig.webglContextAttributes,
cacheControl: unityConfig.cacheControl,
autoSyncPersistentDataPath: unityConfig.autoSyncPersistentDataPath,
setUnityInstance: setUnityInstance,
setLoadingProgression: setLoadingProgression,
setIsLoaded: setIsLoaded,
setInitialisationError: setInitialisationError,
});
/**
* Requests the Unity Instance to enter or exit fullscreen mode.
*/
var requestFullscreen = (0, react_1.useCallback)(function (enabled) { return unityInstance === null || unityInstance === void 0 ? void 0 : unityInstance.SetFullscreen(enabled ? 1 : 0); }, [unityInstance]);
/**
* Requests the Unity Instance to enter pointer lock mode.
* Pointer lock mode allows the Unity Instance to capture mouse movements
* without the cursor leaving the Unity canvas.
*/
var requestPointerLock = (0, react_1.useCallback)(function () { var _a; return (_a = unityInstance === null || unityInstance === void 0 ? void 0 : unityInstance.Module.canvas) === null || _a === void 0 ? void 0 : _a.requestPointerLock(); }, [unityInstance]);
/**
* Sends a message to the Unity Instance to invoke a public method.
*/
var sendMessage = (0, react_1.useCallback)(function (gameObjectName, methodName, parameter) { return unityInstance === null || unityInstance === void 0 ? void 0 : unityInstance.SendMessage(gameObjectName, methodName, parameter); }, [unityInstance]);
/**
* Takes a screenshot of the Unity Instance and returns a base64 encoded
* string.
* @param dataType Defines the type of screenshot to take.
* @param quality Defines the quality of the screenshot.
* @returns A base 64 encoded string of the screenshot.
*/
var takeScreenshot = (0, react_1.useCallback)(function (dataType, quality) { var _a; return (_a = unityInstance === null || unityInstance === void 0 ? void 0 : unityInstance.Module.canvas) === null || _a === void 0 ? void 0 : _a.toDataURL(dataType, quality); }, [unityInstance]);
/**
* Unloads the Unity Instance and cleans up resources.
*/
var unload = (0, react_1.useCallback)(function () { var _a; return (_a = unityInstance === null || unityInstance === void 0 ? void 0 : unityInstance.Quit()) !== null && _a !== void 0 ? _a : Promise.reject(); }, [unityInstance]);
/**
* Gets the metrics information from the Unity Instance.
* This includes performance metrics such as FPS, memory usage, etc.
* @returns A function that returns the metrics information.
*/
var getMetricsInfo = (0, react_1.useCallback)(function () { var _a; return (_a = unityInstance === null || unityInstance === void 0 ? void 0 : unityInstance.GetMetricsInfo) === null || _a === void 0 ? void 0 : _a.call(unityInstance); }, [unityInstance]);
// Initialize the UnityProvider with the provided configuration
// This is where you would typically load the Unity instance
// and set up event listeners, etc.
return {
unityProvider: unityProvider.current,
loadingProgression: loadingProgression,
isLoaded: isLoaded,
initialisationError: initialisationError,
requestFullscreen: requestFullscreen,
requestPointerLock: requestPointerLock,
sendMessage: sendMessage,
takeScreenshot: takeScreenshot,
unload: unload,
getMetricsInfo: getMetricsInfo,
addEventListener: eventSystem.addEventListener,
removeEventListener: eventSystem.removeEventListener,
UNSAFE__unityInstance: unityInstance,
};
};
exports.useUnityContext = useUnityContext;