react-unity-webgl
Advanced tools
Comparing version 8.4.3 to 8.4.4
@@ -23,2 +23,8 @@ import { PureComponent } from "react"; | ||
/** | ||
* A flag representing the component's mount state. | ||
* @private | ||
* @type {boolean} | ||
*/ | ||
private isComponentMounted; | ||
/** | ||
* Event invoked by the UnityInstance when the initialization is progressing. | ||
@@ -45,8 +51,2 @@ * Will be used to track the loading progression and invokes the event listeners | ||
* Initialized the Unity Loader and mounts the UnityInstance to the component. | ||
* During this cycle the unity loader service will append the loader script | ||
* using a new script tag and will continue when the script tag is loaded | ||
* succesfully. Then the Unity Instance Paramters will be constructed, these | ||
* consist out of the spreaded provided unityConfig, optional devicePixelRatio | ||
* and matchWebGLToCanvasSize passed via props. Finally the unity Instance | ||
* will be created. | ||
* @private | ||
@@ -53,0 +53,0 @@ * @async |
@@ -85,2 +85,8 @@ "use strict"; | ||
_this.unityLoaderService = new unity_loader_service_1.UnityLoaderService(); | ||
/** | ||
* A flag representing the component's mount state. | ||
* @private | ||
* @type {boolean} | ||
*/ | ||
_this.isComponentMounted = false; | ||
return _this; | ||
@@ -109,2 +115,3 @@ } | ||
Unity.prototype.componentDidMount = function () { | ||
this.isComponentMounted = true; | ||
this.mountUnityInstance(); | ||
@@ -119,11 +126,6 @@ }; | ||
this.unityContext.quitUnityInstance(); | ||
this.isComponentMounted = false; | ||
}; | ||
/** | ||
* Initialized the Unity Loader and mounts the UnityInstance to the component. | ||
* During this cycle the unity loader service will append the loader script | ||
* using a new script tag and will continue when the script tag is loaded | ||
* succesfully. Then the Unity Instance Paramters will be constructed, these | ||
* consist out of the spreaded provided unityConfig, optional devicePixelRatio | ||
* and matchWebGLToCanvasSize passed via props. Finally the unity Instance | ||
* will be created. | ||
* @private | ||
@@ -141,5 +143,15 @@ * @async | ||
_a.trys.push([0, 3, , 4]); | ||
// Unity requires the loader script to be appended and loaded before we | ||
// can safely instantiate Unity and mount the component. | ||
return [4 /*yield*/, this.unityLoaderService.addFromUrl(this.unityContext.unityConfig.loaderUrl)]; | ||
case 1: | ||
// Unity requires the loader script to be appended and loaded before we | ||
// can safely instantiate Unity and mount the component. | ||
_a.sent(); | ||
// When the loader service was fetching the required scripts, it is | ||
// possible for the component to be unmounted. This is why we check if it | ||
// is still mounted, if not, we do not proceed instantiating Unity. | ||
if (this.isComponentMounted === false) { | ||
return [2 /*return*/]; | ||
} | ||
_unityInstanceParameters = __assign(__assign({}, this.unityContext.unityConfig), { printErr: function (message) { | ||
@@ -158,2 +170,9 @@ return _this.unityContext.dispatchEventListener("error", message); | ||
_unityInstance = _a.sent(); | ||
// Since the creation of the Unity Instance is async, we'll check the | ||
// component's mount state right aftater instantiating. If the component | ||
// is no longer mounted, we'll quit the Unity instance right away. | ||
// HACK requires type cast to boolean due to await between comparisons. | ||
if (this.isComponentMounted === false) | ||
return [2 /*return*/, this.unityContext.quitUnityInstance()]; | ||
// Finally pass the instance back to the context object. | ||
this.unityContext.setUnityInstance(_unityInstance); | ||
@@ -160,0 +179,0 @@ return [3 /*break*/, 4]; |
@@ -35,3 +35,3 @@ import { IUnityConfig } from "../interfaces/unity-config"; | ||
*/ | ||
quitUnityInstance(): void; | ||
quitUnityInstance(): Promise<void>; | ||
/** | ||
@@ -38,0 +38,0 @@ * Sends a message to the UnityInstance to invoke a public method. |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -38,8 +74,17 @@ exports.UnityContext = void 0; | ||
UnityContext.prototype.quitUnityInstance = function () { | ||
var _this = this; | ||
if (typeof this.unityInstance !== "undefined") | ||
this.unityInstance.Quit().then(function () { | ||
_this.dispatchEventListener("quitted"); | ||
_this.unityInstance = undefined; | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!(typeof this.unityInstance !== "undefined")) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.unityInstance.Quit()]; | ||
case 1: | ||
_a.sent(); | ||
this.dispatchEventListener("quitted"); | ||
this.unityInstance = undefined; | ||
_a.label = 2; | ||
case 2: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
@@ -46,0 +91,0 @@ /** |
{ | ||
"name": "react-unity-webgl", | ||
"version": "8.4.3", | ||
"version": "8.4.4", | ||
"description": "React Unity WebGL provides an easy solution for embedding Unity WebGL builds in your React application, with two-way communication between your React and Unity application with advanced API's.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -10,3 +10,3 @@ <div align="center"> | ||
[](https://www.npmjs.com/package/react-unity-webgl) | ||
[](https://github.com/jeffreylanters/react-unity-webgl/actions) | ||
[](https://github.com/jeffreylanters/react-unity-webgl/actions) | ||
<br/> | ||
@@ -28,3 +28,3 @@ [](https://github.com/jeffreylanters/react-unity-webgl/stargazers) | ||
[](https://github.com/sponsors/jeffreylanters) | ||
[](https://github.com/sponsors/jeffreylanters) | ||
@@ -31,0 +31,0 @@ Hi! My name is Jeffrey Lanters, thanks for visiting! This package is an open source hobby project with ongoing development. A result of a long road and endless fight with Unity's updates since 2017, full of sleepless nights, working after hours, and busy weekends. If you're using this module for production, please consider donating to support the project. Thank you! |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
112583
1000