“我有一个Unity3D项目,在那里我可以用叉车开车,我想增加一些功能,比如捡东西,把它放下来,用彩色的材料。但是我很难用统一代码连接按钮(我在index.html中创建的按钮,它是在我使用webgl运行项目时生成的)。webgl页面如下所示:我的项目
我已经在UnityLoader.instantiate上尝试过了,但是它对我不起作用,我得到了以下错误:UnityLoader -误差
这是我的Index.html的脚本
<script>
var container = document.querySelector("#unity-container");
var canvas = document.querySelector("#unity-canvas");
var loadingBar = document.querySelector("#unity-loading-bar");
var progressBarFull = document.querySelector("#unity-progress-bar-full");
var fullscreenButton = document.querySelector("#unity-fullscreen-button");
var warningBanner = document.querySelector("#unity-warning");
function unityShowBanner(msg, type) {
function updateBannerVisibility() {
warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';
}
var div = document.createElement('div');
div.innerHTML = msg;
warningBanner.appendChild(div);
if (type == 'error') div.style = 'background: red; padding: 10px;';
else {
if (type == 'warning') div.style = 'background: yellow; padding: 10px;';
setTimeout(function() {
warningBanner.removeChild(div);``
updateBannerVisibility();
}, 5000);
}
updateBannerVisibility();
}
var buildUrl = "Build";
var loaderUrl = buildUrl + "/build.loader.js";
var config = {
dataUrl: buildUrl + "/build.data",
frameworkUrl: buildUrl + "/build.framework.js",
codeUrl: buildUrl + "/build.wasm",
streamingAssetsUrl: "StreamingAssets",
companyName: "DefaultCompany",
productName: "warehouseOnline3D",
productVersion: "0.1",
showBanner: unityShowBanner,
};
if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
// Mobile device style: fill the whole browser client area with the game canvas:
var meta = document.createElement('meta');
meta.name = 'viewport';
meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
document.getElementsByTagName('head')[0].appendChild(meta);
container.className = "unity-mobile";
// To lower canvas resolution on mobile devices to gain some
// performance, uncomment the following line:
// config.devicePixelRatio = 1;
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
unityShowBanner('WebGL builds are not supported on mobile devices.');
} else {
// Desktop style: Render the game canvas in a window that can be maximized to fullscreen:
canvas.style.width = "960px";
canvas.style.height = "600px";
}
loadingBar.style.display = "block";
var script = document.createElement("script");
script.src = loaderUrl;
script.onload = () => {
createUnityInstance(canvas, config, (progress) => {
progressBarFull.style.width = 100 * progress + "%";
}).then((unityInstance) => {
loadingBar.style.display = "none";
fullscreenButton.onclick = () => {
unityInstance.SetFullscreen(1);
};
}).catch((message) => {
alert(message);
});
};
document.body.appendChild(script);
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/webgl.json");
gameInstance.SendMessage("Sideloader", "test", "printed from webgl");
</script>发布于 2022-11-16 15:33:05
通过UnityLoader afaik的方式来自于非常早期的Unity版本。
您希望将createUnityInstance的结果存储在then块中,然后使用它,如下所示
<script>
let instance = null;
....
createUnityInstance(canvas, config, (progress) => {
progressBarFull.style.width = 100 * progress + "%";
}).then((unityInstance) => {
instance = unityInstance;
...然后,您可以在以后使用它和做
instance.SendMessage("Sideloader", "test", "printed from webgl"); 但是,不能在脚本初始化级别的当前位置执行此操作。你必须等到那个then块真正被调用,然后再为你的按钮做它。
<button onclick='ButtonClick()'>test</button>
...
function ButtonClick()
{
if(instance) instance.SendMessage("Sideloader", "test", "printed from webgl");
}不过,作为一种更复杂的选择,您可以实现一个插件,并让c#部分向其注入一个回调,您以后可以调用它,一旦项目变得更加复杂,您可能会想要这样做。
例如拥有一个MyFancyPlugin.jslib
var myFancyPlugin = {
{
$CallbackPtr : {},
InitializeJavaScript : function(callbackPtr)
{
CallbackPtr = callbackPtr;
}
FancyCall : function(value)
{
const valueSize = lengthBytesUTF8(value)+1;
const valueBuffer = _malloc(dataUrlSize);
stringToUTF8(value, valueBuffer, valueSize);
Runtime.dynCall("vi", $CallbackPtr, [valueBuffer]);
free(valueBuffer);
}
};
autoAddDeps(myFancyPlugin, '$CallbackPtr');
mergInto(LibraryManager.library, myFancyPlugin);在你的按钮上做。
<button onclick='FancyCall("TEST!")'>test</button>然后在c#中有类似的东西。
public static class MyFancyPlugin
{
private delegate void Callback(string value);
[DllImport("__Internal")]
private static void InitializeJavaScript(Callback callback);
public static void Initialize()
{
InitializeJavaScript(OnCallbackReceived);
}
[MonoPInvokeCallback(typeof(Callback))]
private static void OnCallbackReceived(string value)
{
Debug.Log("Received from JavaScript: {value}");
}
}在那里有什么东西需要召唤
MyFancyPlugin.Initialize();当然了
https://stackoverflow.com/questions/74463057
复制相似问题