Unity2021發(fā)布WebGL與網(wǎng)頁交互問題的解決
(一)首先說Unity調(diào)用頁面方法的辦法。
首先是需要在工程的Asset目錄里面建一個Plugins文件夾,然后在文件夾里面創(chuàng)建一個.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的第二個參數(shù)是可以修改的,第二個參數(shù)是一個對象,這個對象里面包含了多個方法的引用,這些方法(例如: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場景列表中的物體的名字,這里注意要保證場景中只有一個叫這個名字的物體,別出現(xiàn)重名的,否則亂套了。methodName是發(fā)消息的方法名,value是方法的參數(shù),這個參數(shù)可以沒有,有的話可以是整數(shù)或者字符串。
具體使用方式參考如下:
unityInstance.SendMessage('MyGameObject', 'MyFunction'); unityInstance.SendMessage('MyGameObject', 'MyFunction', 5); unityInstance.SendMessage('MyGameObject', 'MyFunction', 'MyString');
不過這個unityInstance是內(nèi)部對象(我不知道怎么說這個話比較準(zhǔn)確,暫時先這么說吧。),如果要在外部引用這個對象,頁面代碼請參考如下:
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# Split分隔字符串的應(yīng)用(C#、split、分隔、字符串)
C# Split分隔字符串主要包括用字符串分隔,用多個字符來分隔,用單個字符來分隔等方法實(shí)現(xiàn),下面的具體的實(shí)現(xiàn)代碼2008-11-11HighCharts圖表控件在ASP.NET WebForm中的使用總結(jié)(全)
這篇文章主要介紹了HighCharts圖表控件在ASP.NET WebForm中的使用總結(jié)(全),需要的朋友可以參考下2015-08-08C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別
這篇文章主要給大家介紹了關(guān)于C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09C# CultureInfo之常用InvariantCulture案例詳解
這篇文章主要介紹了C# CultureInfo之常用InvariantCulture案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08