Unity2021发布WebGL与网页交互问题

(一)首先说Unity调用页面方法的办法。

首先是需要在工程的Asset目录里面建一个Plugins文件夹,然后在文件夹里面创建一个.txt文件,名字倒是无所谓,创建好后要把扩展名改成.jslib。文件要包含类似如下内容:

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的第二个参数是可以修改的,第二个参数是一个对象,这个对象里面包含了多个方法的引用,这些方法(例如:Hello()、BingdeWebGLTexture()等)都是在Unity编程中可以引入的。这些方法内调用的方法(例如:wiindow.alert()、GLctx.bindTexture()等)都是将来页面中可以被调用的。

具体在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());
    }
}

(二)其次说说页面方法调用Unity内方法的办法。

简单说就是使用unityInstance发消息就行了。具体方法定义如下:

unityInstance.SendMessage(objectName, methodName, value);

其中的参数objectName是Unity场景列表中的物体的名字,这里注意要保证场景中只有一个叫这个名字的物体,别出现重名的,否则乱套了。methodName是发消息的方法名,value是方法的参数,这个参数可以没有,有的话可以是整数或者字符串。

具体使用方式参考如下:

unityInstance.SendMessage('MyGameObject', 'MyFunction'); 
unityInstance.SendMessage('MyGameObject', 'MyFunction', 5); 
unityInstance.SendMessage('MyGameObject', 'MyFunction', 'MyString');

不过这个unityInstance是内部对象(我不知道怎么说这个话比较准确,暂时先这么说吧。),如果要在外部引用这个对象,页面代码请参考如下:

var myGameInstance = null;
      createUnityInstance(canvas, config).then((unityInstance) => {myGameInstance = unityInstance;});
	  
var SendCmd = function(funName){
		myGameInstance.SendMessage("ZongCai", funName);
}

这样就是使用myGameInstance获得了unityInstance的引用,可以用myGameInstance来发消息了。

官方参考:

WebGL:与浏览器脚本交互 - Unity 手册

猜你喜欢

转载自blog.csdn.net/ttod/article/details/121034263
今日推荐