首页 > 手游开发 > Unity3D > Unity3D WebGL 取消多种浏览器警告
2019
09-27

Unity3D WebGL 取消多种浏览器警告

在使用Unity3D 2018生成的H5,运行时可能会弹出以下几种警告信息:

A.Your browser does not support WebGL
B.Please note that Unity WebGL is not currently supported on mobiles. Press OK if you wish to continue anyway.
C.Please note that your browser is not currently supported for this Unity WebGL content. Press OK if you wish to continue anyway.

为了能够方便的取消这些浏览器警告,可使用以下代码。将以下代码放在位于Assets/Editor/PostBuildActions.cs文件中

using System;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Callbacks;
public class PostBuildActions {

[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget target, string targetPath) {
   var path = Path.Combine(targetPath, "Build/UnityLoader.js");
   var text = File.ReadAllText(path);
   text = Regex.Replace(text, @"compatibilityCheck:function\(e,t,r\)\{.+,Blobs:\{\},loadCode",
       "compatibilityCheck:function(e,t,r){t()},Blobs:{},loadCode");

   File.WriteAllText(path, text);
}
}

这样Unity打包完成后,会自动替代UnityLoader.js文件中警告提示

补充:
在使用Unity 2019重新导出H5后,发现之前写的OnPostProcessBuild已经没有替换compatibilityCheck:function,估计是正则表达式已经有问题。为了能在Unity 2019中取消浏览器警告,重写了这部分代码:

using System;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Callbacks;
public class PostBuildActions {

[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget target, string targetPath) {
   var path = Path.Combine(targetPath, "Build/UnityLoader.js");
   var text = File.ReadAllText(path);
   text = text.Replace("UnityLoader.SystemInfo.mobile", "false");

   File.WriteAllText(path, text);
}
}
最后编辑:
作者:搬运工
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。