在Asset/Plugins文件下创建后缀为 .jslib 的文件,然后进行编写,如下:
mergeInto(LibraryManager.library, {
GetNum: function () {
GetJSNum();
},
GetNumOne: function (str) {
GetJSNumOne(Pointer_stringify(str));
},
GetNumTwo: function (url,id) {
GetJSNumTwo(Pointer_stringify(url),Pointer_stringify(id));
},
});
提供了三个方法,分别不传参,传1个参数,传2个参数,给大家举例子,这里要注意的是,一定要用Pointer_stringify()
转换一下。要不传的参数会变。
然后在C#中调用,如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class WebGLAndJs : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void GetNum();
[DllImport("__Internal")]
private static extern void GetNumOne(string str);
[DllImport("__Internal")]
private static extern void GetNumTwo(string url, string id);
void Start() {
string str = "";
string url = "";
string id = "";
GetNum();
GetNumOne(str);
GetNumTwo(url,id);
}
}
最后在html中增加对应的js方法即可。
- 本文固定链接: http://www.nihao6.cn/archives/481
- 转载请注明: 虚拟世界 于 Unity3D虚拟世界 发表
捐 赠如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝或者微信直接向我捐款,在此非常感谢您对虚拟世界的捐赠。