Skip to content

Commit

Permalink
Merge pull request #171 from zhipeng515/master
Browse files Browse the repository at this point in the history
Fix: Release event listeners for WebGLWindow to prevent page hang dur…
  • Loading branch information
kou-yeung authored Dec 29, 2024
2 parents 7d13f3e + dd64a73 commit 9c819cb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
11 changes: 11 additions & 0 deletions Assets/WebGLSupport/WebGLWindow/WebGLWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ static class WebGLWindowPlugin
#if UNITY_WEBGL && !UNITY_EDITOR
[DllImport("__Internal")]
public static extern void WebGLWindowInit();
[DllImport("__Internal")]
public static extern void WebGLWindowUninit();

[DllImport("__Internal")]
public static extern void WebGLWindowOnFocus(Action cb);

Expand All @@ -35,6 +38,7 @@ static class WebGLWindowPlugin
public static extern bool IsFullscreen();
#else
public static void WebGLWindowInit() { }
public static void WebGLWindowUninit() { }
public static void WebGLWindowOnFocus(Action cb) { }
public static void WebGLWindowOnBlur(Action cb) { }
public static void WebGLWindowOnResize(Action cb) { }
Expand Down Expand Up @@ -66,6 +70,13 @@ static void Init()
WebGLWindowPlugin.WebGLWindowOnBlur(OnWindowBlur);
WebGLWindowPlugin.WebGLWindowOnResize(OnWindowResize);
WebGLWindowPlugin.WebGLWindowInjectFullscreen();

Application.quitting += Uninit;
}
static void Uninit()
{
WebGLWindowPlugin.WebGLWindowUninit();
Application.quitting -= Uninit;
}

[MonoPInvokeCallback(typeof(Action))]
Expand Down
40 changes: 31 additions & 9 deletions Assets/WebGLSupport/WebGLWindow/WebGLWindow.jslib
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
var WebGLWindow = {
focusListener: null,
blurListener: null,
resizeListener: null,

WebGLWindowInit : function() {
// use WebAssembly.Table : makeDynCall
// when enable. dynCall is undefined
Expand All @@ -14,6 +18,21 @@ var WebGLWindow = {
if(typeof Runtime === "undefined") Runtime = { dynCall : dynCall }
}
},
WebGLWindowUninit : function() {
if(focusListener) {
window.removeEventListener('focus', this.focusListener);
this.focusListener = null;
}
if(blurListener) {
window.removeEventListener('blur', this.blurListener);
this.blurListener = null;
}
if(resizeListener) {
window.removeEventListener('resize', this.resizeListener);
this.resizeListener = null;
}
},

WebGLWindowGetCanvasName: function() {
var elements = document.getElementsByTagName('canvas');
var returnStr = "";
Expand All @@ -32,19 +51,22 @@ var WebGLWindow = {
return buffer;
},
WebGLWindowOnFocus: function (cb) {
window.addEventListener('focus', function () {
(!!Runtime.dynCall) ? Runtime.dynCall("v", cb, []) : {{{ makeDynCall("v", "cb") }}}();
});
this.focusListener = function () {
(!!Runtime.dynCall) ? Runtime.dynCall("v", cb, []) : {{{ makeDynCall("v", "cb") }}}();
};
window.addEventListener('focus', this.focusListener);
},
WebGLWindowOnBlur: function (cb) {
window.addEventListener('blur', function () {
(!!Runtime.dynCall) ? Runtime.dynCall("v", cb, []) : {{{ makeDynCall("v", "cb") }}}();
});
this.blurListener = function () {
(!!Runtime.dynCall) ? Runtime.dynCall("v", cb, []) : {{{ makeDynCall("v", "cb") }}}();
};
window.addEventListener('blur', this.blurListener);
},
WebGLWindowOnResize: function(cb) {
window.addEventListener('resize', function () {
(!!Runtime.dynCall) ? Runtime.dynCall("v", cb, []) : {{{ makeDynCall("v", "cb") }}}();
});
this.resizeListener = function () {
(!!Runtime.dynCall) ? Runtime.dynCall("v", cb, []) : {{{ makeDynCall("v", "cb") }}}();
};
window.addEventListener('resize', this.resizeListener);
},
WebGLWindowInjectFullscreen : function () {
document.makeFullscreen = function (id, keepAspectRatio) {
Expand Down

0 comments on commit 9c819cb

Please sign in to comment.