首页 > 解决方案 > Unity WebGL 覆盖兼容性检查回调

问题描述

我目前正在开发一个 Unity WebGL 项目,该项目仅支持WebGL 2.0. 该项目不适用于WebGL 1.0.

现在我想意识到,如果浏览器不支持WebGL 2.0,将显示图像而不是 WebGL 上下文。

如果不支持 WebGL,则该UnityLoader.instantiate()函数有一个回调函数。不幸的是,我的代码没有在这个函数中调用。

    UnityLoader.instantiate("unityContainer", "Build/Build.json", {
        compatibilityCheck: function(unityInstance, onsuccess, onerror) {
            if (!UnityLoader.SystemInfo.hasWebGL) {
                unityInstance.popup("Your browser does not support WebGL", [{text: "OK", callback: onerror}]);
                document.getElementById("unityContainer").style.display = "none";
                document.getElementById("fallbackHeader").style.display = "block";
            }

提前致谢!最好的问候,劳伦斯·特里彭

标签: javascriptunity3dwebglunity-webgl

解决方案


你可以试试

const supportsWebGL2 = !!document.createElement('canvas').getContext('webgl2');

因此,也许将您的 WebGL 模板更改为类似

const supportsWebGL2 = !!document.createElement('canvas').getContext('webgl2');
if (!supportsWebGL2) {
   // do something to display message
} else {
   UnityLoader.instantiate("unityContainer", "Build/Build.json", {
        compatibilityCheck: function(unityInstance, onsuccess, onerror) {
            if (!UnityLoader.SystemInfo.hasWebGL) {
                unityInstance.popup("Your browser does not support WebGL", [{text: "OK", callback: onerror}]);
                document.getElementById("unityContainer").style.display = "none";
                document.getElementById("fallbackHeader").style.display = "block";
            }

   ...

推荐阅读