Setup: function (strJsonDataPtr) {
try {
if (typeof UTF8ToString !== 'function') {
throw new Error("UTF8ToString is not defined.");
}
const strJsonData = UTF8ToString(strJsonDataPtr);
console.log("[WebGL] Setup data received from Unity:", strJsonData);
const setupOptions = JSON.parse(strJsonData);
// 动态加载 SDK
var script = document.createElement('script');
script.src = 'Build/main.js'; // 修改为正确路径
script.type = 'text/javascript';
document.body.appendChild(script);
console.log("[WebGL] Loading SDK script...");
script.onload = function () {
console.log("11111111111111");
try {
console.log("[WebGL] SDK script successfully loaded.");
if (typeof SDK === 'undefined') {
throw new Error("SDK is not loaded.");
}
if (typeof SDK .Manager !== 'function') {
throw new Error("SDK .Manager is not defined or is not a function.");
}
const instance = new SDK .Manager();
console.log("[WebGL] Setup options:", setupOptions);
// 调用 SDK 的 setup 方法,采用回调方式处理结果
instance.setup(setupOptions, function (result) {
if (result.success) {
console.log("[WebGL] Setup completed successfully:", result);
const successMessage = JSON.stringify({ result: true });
SendMessage("WebGL", "_SetupSucceed", successMessage);
} else {
console.error("[WebGL] Setup failed:", result.error);
const failureMessage = JSON.stringify({
result: false,
error: result.error || "SDK initialization failed",
});
SendMessage("WebGL", "_SetupFailed", failureMessage);
}
});
} catch (error) {
console.error("[WebGL] Error during SDK Setup:", error);
const errorMessage = JSON.stringify({
result: false,
error: error.message,
});
SendMessage("WebGL", "_SetupFailed", errorMessage);
}
};
console.log("2222222222222222");
script.onerror = function (errorEvent) {
console.error("[WebGL] Failed to load SDK script:", errorEvent);
const errorMessage = JSON.stringify({
result: false,
error: "Failed to load SDK script",
});
SendMessage("WebGL", "_SetupFailed", errorMessage);
};
console.log("33333333333333333");
} catch (error) {
console.log("444444444444444444");
console.error("[WebGL] Error during setup:", error);
const errorMessage = JSON.stringify({
result: false,
error: error.message,
});
SendMessage("WebGL", "_SetupFailed", errorMessage);
}
},
为什么 script.onload = function (){} 里面的代码不会执行??我打包为 webgl 在浏览器中都能正常运行,但是 Unity 中使用微信小游戏的一键打包导入微信小游戏之后就无法执行 script.onload = function (){} 里面的代码。。