You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity WebXR构建在Quest浏览器中无法渲染(此前正常)

问题描述

我使用Unity 6.3(6000.3.19.f1),基于VR模板场景,通过WebGL平台结合WebXR导出工具开发VR网页应用。目前仅通过WebGLTemplate HTML文件实现点击按钮请求XR会话的功能,场景无额外逻辑。此前该VR体验会触发浏览器权限请求,随后正常启动沉浸式VR体验,但现在卡在带有三个加载点的粉紫色开放世界加载界面。排查提交记录未找到正常构建与当前构建的差异,且无任何警告或错误信息可供排查。

以下是请求XR体验的代码:

var script = document.createElement("script");
script.src = loaderUrl;
script.onload = () => {
    createUnityInstance(canvas, config, (progress) => {
        document.querySelector("#unity-progress-bar-full").style.width = 100 * progress + "%";
    }).then((instance) => {
            instat(instance);
    }).catch((message) => {
        alert(message);
    });
};
document.body.appendChild(script);

let xr = null;
let xrSession = null;
let webxrPolyfill = null;

let enterVRButton = document.getElementById('entervr');


const instat = function (instance) {
    unityInstance = instance;

    xr = getXR();

    if (xr) {
        xr.isSessionSupported("immersive-vr").then((immersiveOK) => {
            if (immersiveOK) {
                enterVRButton.disabled = false;
                enterVRButton.innerText = "VR Enabled";
            } else {
                enableVR(false);
            }
        })
    } else {
        enableVR(false);
    }
}

enterVRButton.onclick = function () {
    xr.requestSession("immersive-vr").then((session) => {
        xrSession = session;
        enableVR(true);

        xrSession.onend = (event) => {
            enableVR(false);
        };
    });
}

function enableVR(enabled) {
    if (enabled) {
        unityInstance.SendMessage("XREnabler", "Enable", 1);
    } else {
        unityInstance.SendMessage("XREnabler", "Enable", 0);
    }
}

function getXR() {
    let tempXR;

    tempXR = navigator.xr;
    if (!tempXR) {
        webxrPolyfill = new WebXRPolyfill();
        tempXR = webxrPolyfill;
    }

    return tempXR;
}
排查与解决方案

1. 补全XR会话的错误捕获逻辑

当前代码未处理xr.requestSession的失败场景,会话请求异常可能静默卡住流程。修改按钮点击事件,添加错误捕获:

enterVRButton.onclick = function () {
    xr.requestSession("immersive-vr").then((session) => {
        xrSession = session;
        enableVR(true);

        xrSession.onend = (event) => {
            enableVR(false);
        };
    }).catch((err) => {
        console.error("XR会话请求失败:", err);
        alert(`VR启动失败: ${err.message}`);
    });
}

打开浏览器控制台(F12)查看具体错误信息,这是定位问题的核心入口。

2. 验证Unity实例调用的有效性

  • 确认场景中存在名称完全匹配的XREnabler GameObject,且该对象挂载的脚本包含Enable(int)方法(方法名大小写严格区分)。
  • 在enableVR函数中添加日志,确认调用是否执行:
function enableVR(enabled) {
    console.log("触发VR状态切换:", enabled);
    if (enabled) {
        unityInstance.SendMessage("XREnabler", "Enable", 1);
    } else {
        unityInstance.SendMessage("XREnabler", "Enable", 0);
    }
}

3. 排查浏览器环境问题

  • 确保使用Chrome、Edge等支持WebXR的最新版浏览器,避免旧版本兼容性问题。
  • 清除浏览器缓存与站点权限,切换到隐私模式重新加载页面,排除扩展或隐私设置干扰。
  • 检查设备VR权限是否被禁用,手动在浏览器设置中重置站点的XR权限。

4. 重新校验Unity构建配置

  • 进入Edit > Project Settings > Player > WebGL > XR Settings,确认WebXR选项已启用。
  • 执行Clean Build重新构建项目,避免构建缓存残留导致的异常。
  • 尝试回退到之前正常的Unity版本测试,排查是否为Unity 6.3版本的兼容性问题。

5. 修复WebXR Polyfill加载逻辑

当前Polyfill初始化未处理加载失败场景,修改getXR函数并确保脚本已正确引入:

function getXR() {
    let tempXR = navigator.xr;
    if (!tempXR) {
        try {
            webxrPolyfill = new WebXRPolyfill();
            tempXR = webxrPolyfill;
            console.log("WebXR Polyfill已加载");
        } catch (err) {
            console.error("WebXR Polyfill加载失败:", err);
            return null;
        }
    }
    return tempXR;
}

同时确认HTML文件中已正确引入WebXR Polyfill的脚本资源。


内容的提问来源于stack exchange,提问作者Samson Afolabi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 12:23:10