Unity——WegGL打包問題

小紫蘇發表於2021-12-10

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 = "88c8f3e4c36fce74c59a1de4fda281af25b3165a2e9e080588a693d1b839b771";

      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:

image-20211210213110421

Splash Image

開屏動畫,載入條,icon等素材替換;

image-20211210213300026

開屏Logo把DrawMode換成順序,然後換順序放上Logo,點Preview可以預覽;

WebGL模板資料夾TemplateData下有個css,定義了載入進度條和logo格式,同時也存放了相應圖片素材,直接替換或者修改css都可;

圖示.ico也在裡面,png轉ico要轉格式;

相關文章