Unity发布webgl的一些问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linxinfa/article/details/80363671

1 技术概览

为了在WebGL中运行,所有代码需要是JavaScript。我们使用 emscripten 编译器工具链来交叉编译Unity运行时代码(用C和C++编写)到asm.js的JavaScript代码。asm.js是一个非常理想的JavaScript子集,允许JavaScript引擎 预编译asm.js代码为极具性能的原生代码。 
为了转换.NET 游戏代码(C#和UnityScript脚本)至JavaScript,我们使用了一个称为IL2CPP的技术。IL2CPP获取.NET字节代码并将它转换为对应的C++源文件,然后通过使用emscripten,这些源文件被编译,最终将用户的脚本转换为JavaScript。

Unity WebGL 取消浏览器警告

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:functione,t,re,t,r\{.+,Blobs:\{\},loadCode",  
            "compatibilityCheck:function(e,t,r){t()},Blobs:{},loadCode");  
        File.WriteAllText(path, text);  
    }  
}  

Unity打包完毕后,修改UnityLoader.js文件,进行文本替换。

相关警告提示:

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

3 部署IIS教程

在Unity3D中发布WebGL,当我们需要将其加入我们自己的网站时,需要将WebGL的内容放入服务代码,我们需要创建IIS服务器

部署IIS教程:点击打开链接

修复Uncaught SyntaxError: Unexpected token < in JSON at position 0

发布成webgl后,放在iis根目录中,然后再在根目录中放置Web.config文件

其中Web.config的内容如下:

<configuration>
  <system.webServer>
      <staticContent>
            <remove fileExtension=".mem" />
      <remove fileExtension=".data" />
      <remove fileExtension=".unity3d" />
      <remove fileExtension=".jsbr" />
      <remove fileExtension=".membr" />
      <remove fileExtension=".databr" />
      <remove fileExtension=".unity3dbr" />
      <remove fileExtension=".jsgz" />
      <remove fileExtension=".memgz" />
      <remove fileExtension=".datagz" />
      <remove fileExtension=".unity3dgz" />
      <remove fileExtension=".json" />
      <remove fileExtension=".unityweb" />
 
      <mimeMap fileExtension=".mem" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".data" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".unity3d" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".jsbr" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".membr" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".databr" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".unity3dbr" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".jsgz" mimeType="application/x-javascript; charset=UTF-8" />
      <mimeMap fileExtension=".memgz" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".datagz" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".unity3dgz" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".json" mimeType="application/json; charset=UTF-8" />
      <mimeMap fileExtension=".unityweb" mimeType="application/octet-stream" />
    </staticContent>
  </system.webServer>
</configuration>

阅读更多

没有更多推荐了,返回首页