Unity打包WebGL所遇到的坑!!!

WebGL打包过程可真是异常坎坷,开发过程中会有各种坑,大概总结了一下,记录下来。

网络请求问题:

1.Http请求使用C#中请求方式,是不支持的,必须使用Unity的Http请求UnityWebRequest/WWW方式。

 using (var webRequest = UnityWebRequest.Get("https://www.baidu.com/"))
     {
         yield return webRequest.SendWebRequest();
         if (!webRequest.isNetworkError && !webRequest.isHttpError)
             Debug.Log(webRequest.downloadHandler.text);
     }
 using (var webRequest = UnityWebRequest.Post("https://www.baidu.com/","参数"))
     {
         yield return webRequest.SendWebRequest();
         if (!webRequest.isNetworkError && !webRequest.isHttpError)
             Debug.Log(webRequest.downloadHandler.text);
     }

2.经常出现的问题就是跨域问题,开发过Web的人员都知道怎么解决,对应的返回头加上Access-Control-Allow-Origin

3.json的序列化和反序列化,使用JSON.NET库没毛病吧,但是下面这样方式就不可以;

string json = "json字符串";Dictionary<string, string> dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
这样没毛病,一切OK,但是要按照下面这样写,就会踩雷

public class Dic{
    public int code { get; set; }
    public Dictionary<string, string> data { get; set; }

    public static Dic DeDic(string json)
    {
        return JsonConvert.DeserializeObject<Dic>(json);
    }}

如果是按照上面这样写,恭喜你,打包出来的东西肯定报错,反序列化得到的全都是null。
最后发现问题的解释:

While many in the Unity Community have succeeded in getting JSON .NET to work for their games, it has never worked properly with iOS or IL2CPP. The iOS errors are due to incompatibilities with AOT (Ahead of Time Compilation) that is used by Mono/IL2CPP in iOS.

就一个意思,兼容问题不支持。

4.关于出现将打包出来的项目发布到本地Web服务,然后通过网页访问出现WebGL build error :Uncaught SyntaxError: Unexpected token < || UnityLoader.js is not a function (SOLVED)错误
Unity打包WebGL所遇到的坑!!!

解决办法:
①新建一个名为文本文件
②打开该文本文件,然后给该文件添加如下内容

 <configuration>
        <system.webServer>
           <staticContent>
              <mimeMap fileExtension=".json" mimeType ="text/json" />
              <mimeMap fileExtension=".unity3d" mimeType ="application/octet-stream" />
              <mimeMap fileExtension=".unityweb" mimeType ="application/binary" />
           </staticContent>
        </system.webServer> 
</configuration>

③保存该文本文件内容
④将该文本文件修改为web.config文件保存,如下所示:
Unity打包WebGL所遇到的坑!!!
⑤将该文本文件放置打包出来的根目录下面即可(也就是和index.html同级别),如下图所示:Unity打包WebGL所遇到的坑!!!

打包过程中编译出现错误:

打包出现:
Unity打包WebGL所遇到的坑!!!

这种问题有么有?有么有?最后发现打包的路径不能有中文,工程路径也不能有中文,太气人!!!

**打包结束,index.html改变自适应浏览器

添加:**

<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

 <script>
      var gameInstance = UnityLoader.instantiate("gameContainer", "Build/Build.json", {onProgress: UnityProgress});

    function autoAdj(){
            var canvas = document.getElementById("#canvas");
            canvas.height=document.documentElement.clientHeight*0.99;
            canvas.width=document.documentElement.clientWidth*0.99;
            $("#gameContainer").height(document.documentElement.clientHeight*0.99);
            $("#gameContainer").width(document.documentElement.clientWidth*0.99);
        }
</script>

<body onload="autoAdj()" onresize = "autoAdj()">
<div class="webgl-content">
。。。。。。。
</div>
</body>

猜你喜欢

转载自blog.51cto.com/myselfdream/2507184