先看一下官方提供的最新的使用方法https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
下面是我项目里的实际应用
创建一个myjs.jslib的文件,将下面的东西复制到该文件里面
mergeInto(LibraryManager.library, {
GetUA: function () {
var uA = navigator.userAgent.toLowerCase();
var ipad = uA.match(/ipad/i) == "ipad";
var iphone = uA.match(/iphone os/i) == "iphone os";
var midp = uA.match(/midp/i) == "midp";
var uc7 = uA.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var uc = uA.match(/ucweb/i) == "ucweb";
var android = uA.match(/android/i) == "android";
var windowsce = uA.match(/windows ce/i) == "windows ce";
var windowsmd = uA.match(/windows mobile/i) == "windows mobile";
if (!(ipad || iphone || midp || uc7 || uc || android || windowsce || windowsmd)) {
// PC 端 返回1
return 1;
}else{
// 移动端 返回2
return 2;
}
},
});
在Unity里面新建一个脚本
[DllImport("__Internal")]
private static extern int GetUA();
private void OnGUI()
{
int a = GetUA();
GUIStyle gUIStyle = new GUIStyle();
gUIStyle.fontSize = 90;
GUI.Label(new Rect(500, 200, 500, 500), a.ToString(), gUIStyle);
}
然后切换到webgl平台发布就可以了
PC端效果 如下
移动端效果 如下
最开始的时候,我也遇到过发布就会爆出很多错的情况,但是只遇到过一次,之后不管再怎么发布,也都在没遇到过了,所以关于发布为什么为报错,我也不是很清楚原因