`我有一個 Unity 3D 專案,我可以在其中使用叉車四處行駛,我想添加一些功能,例如拾取東西、放下東西和給材料著色。但是我在使用我的統一代碼連接按鈕(我在 index.html 中創建,它是在我使用 webgl 運行我的專案時生成的)時遇到問題。webgl 頁面如下所示: 我的項??目
我已經用 UnityLoader.instantiate 試過了,但它對我不起作用我收到以下錯誤: UnityLoader - error
這是我的 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>
應該從 UnityLoader 呼叫的函式
uj5u.com熱心網友回復:
通過UnityLoaderafaik 的方式來自非常早期的 Unity WebGL 版本。
您想要將結果存盤在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://www.uj5u.com/net/536479.html
標籤:javascriptC#网页格式unity3dwebgl
上一篇:UnityhttpWebRequest將資料發布/發送到輸入欄位
下一篇:Unity2DRPG藥水治療
