Unity——WegGL打包问题

发布于 2022年 01月 12日 11:08

Rendering设置

Gamma和Linear颜色空间,两者有色差,Gamma有个2.25左右的修正值;

WebGL2.0可用的情况,只支持Deferred Render延迟渲染,且只支持Linear颜色空间;

UnityWebGL使用Video播放工具还不支持WegGL2.0;

使用WebGL1.0对shader有很大的限制,如果shader失效替换WebGL2.0,或者调低shader版本;

这里的设置还关系到屏幕后期处理PostProcessing能不能用;

OtherSetting

Strip Engine Code和Managed Stripping Level大概是什么代码剥离和剥离等级,打包后运行出错有可能就是这里选择了enable;

Prebake Collision Mesh:提前添加碰撞到网格,变复杂场景加载过慢问题,空间换时间;问题

Optimize Mesh Data:静态分析材质,去掉Mesh中无用的数据,比如切线,多余UV什么的;但是如果有代码动态切换材质使用法线的,打包运行会出错,尽量也别选吧;

PublishingSettings

Enable Exceptions 是否允许打印日志,测试选择打印,报错会使用trycatch,至少能让程序跑起来;

Compression Format 发布版本文件压缩格式,如果出错也可试试看是不是因为压缩问题;

Name Files As Hashes 使用MD5哈希作为每个文件名,在Js中再处理也要用哈希名;

Data caching 允许浏览器缓存,可能需要浏览器权限;

WebGL模板

官方给了mini和Default模板,在Unity安装目录PlaybackEngines/WebGLSupport/BuildTools/WebGLTemplates/中;

可以拷贝官方的模板做修改也放在这个目录,会在playersetting中显示出自定义模板;

修改模板中Index.html即可;

下面是自定义做了浏览器自适应的模板,取消了白色加载条;

<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Unity WebGL Player | {{{ PRODUCT_NAME }}}</title>
    <link rel="shortcut icon" href="TemplateData/favicon.ico">
    <link rel="stylesheet" href="TemplateData/style.css">
		<style>
	html,body{overflow:hidden;}
	</style>
  </head>
  <body>
    <div id="unity-container" class="unity-desktop">
      <canvas id="unity-canvas" width={{{ WIDTH }}} height={{{ HEIGHT }}}></canvas>
      <div id="unity-loading-bar">
        <div id="unity-logo"></div>
        <div id="unity-progress-bar-empty">
          <div id="unity-progress-bar-full"></div>
        </div>
      </div>
      <div id="unity-mobile-warning">
        WebGL builds are not supported on mobile devices.
      </div>
    </div>
    <script>
      var buildUrl = "Build";
      var loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";
      var config = {
        dataUrl: buildUrl + "/{{{ DATA_FILENAME }}}",
        frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}",
        codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",
#if MEMORY_FILENAME
        memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",
#endif
#if SYMBOLS_FILENAME
        symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",
#endif
        streamingAssetsUrl: "StreamingAssets",
        companyName: "{{{ COMPANY_NAME }}}",
        productName: "{{{ PRODUCT_NAME }}}",
        productVersion: "{{{ PRODUCT_VERSION }}}",
      };

      var container = document.querySelector("#unity-container");
      var canvas = document.querySelector("#unity-canvas");
      var loadingBar = document.querySelector("#unity-loading-bar");
      var progressBarFull = document.querySelector("#unity-progress-bar-full");  
      var mobileWarning = document.querySelector("#unity-mobile-warning");

      // By default Unity keeps WebGL canvas render target size matched with
      // the DOM size of the canvas element (scaled by window.devicePixelRatio)
      // Set this to false if you want to decouple this synchronization from
      // happening inside the engine, and you would instead like to size up
      // the canvas DOM size and WebGL render target sizes yourself.
      // config.matchWebGLToCanvasSize = false;

	  //监听浏览器宽度的改变
      let width =0;
      let height=0;
      function changeMargin() {
        width = document.documentElement.clientWidth
        height = document.documentElement.clientHeight + 8
		canvas.style.width = width+"px";
        canvas.style.height = height+"px";
      }

      window.onresize = function () {
          changeMargin();
      }

      if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
        container.className = "unity-mobile";
        // Avoid draining fillrate performance on mobile devices,
        // and default/override low DPI mode on mobile browsers.
        config.devicePixelRatio = 1;
        mobileWarning.style.display = "block";
        setTimeout(() => {
          mobileWarning.style.display = "none";
        }, 5000);
      } else {
		changeMargin();
      }
#if BACKGROUND_FILENAME
      canvas.style.background = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover";
#endif
      loadingBar.style.display = "block";

	  let address = "8771";

      var script = document.createElement("script");
      script.src = loaderUrl;
      script.onload = () => {
        createUnityInstance(canvas, config, (progress) => {
          progressBarFull.style.width = 100 * progress + "%";
        }).then((unityInstance) => {
          loadingBar.style.display = "none";       
		  
		   unityInstance.SendMessage("GameInit","LoginByWallet",address);  
		  
        }).catch((message) => {
          alert(message);
        });
      };
      document.body.appendChild(script);
    </script>
  </body>
</html>

打开日志和栈信息

OtherSetting中如下,以及上述的PublishingSetting中Enable Exceptions:

Splash Image

开屏动画,加载条,icon等素材替换;

开屏Logo把DrawMode换成顺序,然后换顺序放上Logo,点Preview可以预览;

WebGL模板文件夹TemplateData下有个css,定义了加载进度条和logo格式,同时也存放了相应图片素材,直接替换或者修改css都可;

图标.ico也在里面,png转ico要转格式;

推荐文章