Unity2021發(fā)布WebGL與網(wǎng)頁交互問題的解決
(一)首先說Unity調(diào)用頁面方法的辦法。
首先是需要在工程的Asset目錄里面建一個(gè)Plugins文件夾,然后在文件夾里面創(chuàng)建一個(gè).txt文件,名字倒是無所謂,創(chuàng)建好后要把擴(kuò)展名改成.jslib。文件要包含類似如下內(nèi)容:
mergeInto(LibraryManager.library, {
Hello: function () {
window.alert("Hello, world!");
},
HelloString: function (str) {
window.alert(Pointer_stringify(str));
},
PrintFloatArray: function (array, size) {
for(var i = 0; i < size; i++)
console.log(HEAPF32[(array >> 2) + i]);
},
AddNumbers: function (x, y) {
return x + y;
},
StringReturnValueFunction: function () {
var returnStr = "bla";
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
return buffer;
},
BindWebGLTexture: function (texture) {
GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
},
});
這其中只有mergeInto的第二個(gè)參數(shù)是可以修改的,第二個(gè)參數(shù)是一個(gè)對象,這個(gè)對象里面包含了多個(gè)方法的引用,這些方法(例如:Hello()、BingdeWebGLTexture()等)都是在Unity編程中可以引入的。這些方法內(nèi)調(diào)用的方法(例如:wiindow.alert()、GLctx.bindTexture()等)都是將來頁面中可以被調(diào)用的。
具體在Unity編程中引入方法的方式以C#為例:
首先需要引入命名空間:
using System.Runtime.InteropServices;
其次需要寫具體引入代碼:
[DllImport("__Internal")] private static extern void Hello();
參考以下代碼引入和使用示例
using UnityEngine;
using System.Runtime.InteropServices;
public class NewBehaviourScript : MonoBehaviour {
[DllImport("__Internal")]
private static extern void Hello();
[DllImport("__Internal")]
private static extern void HelloString(string str);
[DllImport("__Internal")]
private static extern void PrintFloatArray(float[] array, int size);
[DllImport("__Internal")]
private static extern int AddNumbers(int x, int y);
[DllImport("__Internal")]
private static extern string StringReturnValueFunction();
[DllImport("__Internal")]
private static extern void BindWebGLTexture(int texture);
void Start() {
Hello();
HelloString("This is a string.");
float[] myArray = new float[10];
PrintFloatArray(myArray, myArray.Length);
int result = AddNumbers(5, 7);
Debug.Log(result);
Debug.Log(StringReturnValueFunction());
var texture = new Texture2D(0, 0, TextureFormat.ARGB32, false);
BindWebGLTexture(texture.GetNativeTextureID());
}
}
(二)其次說說頁面方法調(diào)用Unity內(nèi)方法的辦法。
簡單說就是使用unityInstance發(fā)消息就行了。具體方法定義如下:
unityInstance.SendMessage(objectName, methodName, value);
其中的參數(shù)objectName是Unity場景列表中的物體的名字,這里注意要保證場景中只有一個(gè)叫這個(gè)名字的物體,別出現(xiàn)重名的,否則亂套了。methodName是發(fā)消息的方法名,value是方法的參數(shù),這個(gè)參數(shù)可以沒有,有的話可以是整數(shù)或者字符串。
具體使用方式參考如下:
unityInstance.SendMessage('MyGameObject', 'MyFunction');
unityInstance.SendMessage('MyGameObject', 'MyFunction', 5);
unityInstance.SendMessage('MyGameObject', 'MyFunction', 'MyString');
不過這個(gè)unityInstance是內(nèi)部對象(我不知道怎么說這個(gè)話比較準(zhǔn)確,暫時(shí)先這么說吧。),如果要在外部引用這個(gè)對象,頁面代碼請參考如下:
var myGameInstance = null;
createUnityInstance(canvas, config).then((unityInstance) => {myGameInstance = unityInstance;});
var SendCmd = function(funName){
myGameInstance.SendMessage("ZongCai", funName);
}
這樣就是使用myGameInstance獲得了unityInstance的引用,可以用myGameInstance來發(fā)消息了。
官方參考:
到此這篇關(guān)于Unity2021發(fā)布WebGL與網(wǎng)頁交互問題的解決的文章就介紹到這了,更多相關(guān)Unity2021發(fā)布WebGL與網(wǎng)頁交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)鼠標(biāo)移動(dòng)到曲線圖上顯示值的方法
這篇文章主要介紹了C#實(shí)現(xiàn)鼠標(biāo)移動(dòng)到曲線圖上顯示值的方法,是C#的WinForm窗體程序設(shè)計(jì)中非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10
C#保存listbox中數(shù)據(jù)到文本文件的方法
這篇文章主要介紹了C#保存listbox中數(shù)據(jù)到文本文件的方法,涉及C#操作listbox數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04
Visual Studio C#創(chuàng)建windows服務(wù)程序
用Visual C#創(chuàng)建Windows服務(wù)不是一件困難的事,本文就將指導(dǎo)你一步一步創(chuàng)建一個(gè)Windows服務(wù)并使用它,本文主要介紹了Visual Studio C#創(chuàng)建windows服務(wù)程序,感興趣的可以了解一下2024-01-01
C# DataGridView綁定數(shù)據(jù)源的方法
這篇文章主要為大家詳細(xì)介紹了C# DataGridView綁定數(shù)據(jù)源的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09

