您当前的位置: 首页 >  unity
  • 7浏览

    0关注

    157博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity WebGL开发

牙膏上的小苏打2333 发布时间:2020-05-12 09:40:09 ,浏览量:7

教程来啦
  • 一.WebGL不能干的那些事
  • 二.注意事项
    • 1.播放声音
    • 2.跨域
    • 3.发布http or https?
    • 4.包体大小
    • 5.Odin
    • 6.T序列化失败
  • 三.参考资料
  • 四.Build
    • 主要步骤
    • web.config
      • Apache
      • IIS
    • 手机端去掉提示框
    • 全屏
    • 加载时间过长
    • 页面模板
      • 只要进度条不要Logo
      • 自定义加载背景图
    • 激活嵌入资源
  • 五.IIS 部署
    • 启用Internet Infomation Services
    • 添加程序到IIS
  • 六.浏览器设置
  • 七.UnityScript与JavaScript通信

一.WebGL不能干的那些事

1.内置的video player 无法播放。(可以使用AVPro 播放) 2.多线程 3.socket(可以用UnityWebRequest或websocket) 4.不支持dynamic类型,可以使用object。 5.不支持ComputeShader: SystemInfo.supportsComputeShaders 在这里插入图片描述

二.注意事项 1.播放声音

要去掉AutoPlay

2.跨域

参考1 参考2 官网 web.config添加

		
			
				
				
				
				
			
		

在这里插入图片描述

3.发布http or https?

当发布到https上时程序中访问http的都会被改成https,这时候一般就报错了。。。

4.包体大小

注意工程中尽量不用Resources文件夹,不然打包的时候会导致包体过大。因为放在该文件夹下的东西都会被打包。

是否包含Resourcesbuild包体大小包含Resources14M无Resources12.7M无Resource+Assembly Definitions8.93M 5.Odin

有的类型在WebGL平台并不支持序列化,如果你使用了Odin就需要在打包前生成AOT: 在这里插入图片描述 在这里插入图片描述

6.T序列化失败
T序列化失败,System.Reflection.Emit.DynamicMethod::.ctor

把序列化工具换成Json .Net For Unity

三.参考资料

hi,WebGL Unity 发布WebGL注意事项 Unity WebGL 官方教程 Unity WebGL官方教程翻译 发布后打不开 Unity WebGL游戏 官方WebGL Tiny IIS的搭建 别个的笔记 问题总结

四.Build

空场景build耗时~142s 测试demo

主要步骤
#mermaid-svg-BLhCfcekjSKkHLaJ {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-BLhCfcekjSKkHLaJ .error-icon{fill:#552222;}#mermaid-svg-BLhCfcekjSKkHLaJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-BLhCfcekjSKkHLaJ .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-BLhCfcekjSKkHLaJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-BLhCfcekjSKkHLaJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-BLhCfcekjSKkHLaJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-BLhCfcekjSKkHLaJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-BLhCfcekjSKkHLaJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-BLhCfcekjSKkHLaJ .marker.cross{stroke:#333333;}#mermaid-svg-BLhCfcekjSKkHLaJ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-BLhCfcekjSKkHLaJ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-BLhCfcekjSKkHLaJ .cluster-label text{fill:#333;}#mermaid-svg-BLhCfcekjSKkHLaJ .cluster-label span{color:#333;}#mermaid-svg-BLhCfcekjSKkHLaJ .label text,#mermaid-svg-BLhCfcekjSKkHLaJ span{fill:#333;color:#333;}#mermaid-svg-BLhCfcekjSKkHLaJ .node rect,#mermaid-svg-BLhCfcekjSKkHLaJ .node circle,#mermaid-svg-BLhCfcekjSKkHLaJ .node ellipse,#mermaid-svg-BLhCfcekjSKkHLaJ .node polygon,#mermaid-svg-BLhCfcekjSKkHLaJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-BLhCfcekjSKkHLaJ .node .label{text-align:center;}#mermaid-svg-BLhCfcekjSKkHLaJ .node.clickable{cursor:pointer;}#mermaid-svg-BLhCfcekjSKkHLaJ .arrowheadPath{fill:#333333;}#mermaid-svg-BLhCfcekjSKkHLaJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-BLhCfcekjSKkHLaJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-BLhCfcekjSKkHLaJ .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-BLhCfcekjSKkHLaJ .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-BLhCfcekjSKkHLaJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-BLhCfcekjSKkHLaJ .cluster text{fill:#333;}#mermaid-svg-BLhCfcekjSKkHLaJ .cluster span{color:#333;}#mermaid-svg-BLhCfcekjSKkHLaJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-BLhCfcekjSKkHLaJ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
Build后目录配置
配置并Build
web.config
手机端去掉提示框
选择模板
自定义模板
勾选WebAssembly Streaming
web.config

需要在index.html同级目录下添加web.config配置文件。 需要根据部署的服务器决定:

Apache

web.config后缀改成.htaccess Gzip版本:


  AddType application/octet-stream .unityweb
  AddEncoding gzip .unityweb
  AddType application/wasm .wasm
  AddEncoding gzip .wasm
  AddOutputFilterByType DEFLATE application/wasm


Brotli版本


  AddEncoding br .unityweb
  RemoveType .wasm
  AddType application/wasm .wasm
  RemoveType .unityweb
  AddType application/octet-stream .unityweb

IIS

首先安装URL Rewrite



  
    
      
      
      
      
    
    
      
        
          
          
            
          
          
        
      
    
  

手机端去掉提示框

在手机端打开wegl项目会弹出不支持webgl的提示框,不想要的话可以把UnityLoader.js文件内的提示框代码改下(line:2041): 修改前代码

compatibilityCheck: function(e, t, r) {
    UnityLoader.SystemInfo.hasWebGL ? UnityLoader.SystemInfo.mobile ? e.popup("Please note that Unity WebGL is not currently supported on mobiles. Press OK if you wish to continue anyway.", [{
      text: "OK",
      callback: t
    }]) : ["Edge", "Firefox", "Chrome", "Safari"].indexOf(UnityLoader.SystemInfo.browser) == -1 ? e.popup("Please note that your browser is not currently supported for this Unity WebGL content. Press OK if you wish to continue anyway.", [{
      text: "OK",
      callback: t
    }]) : t() : e.popup("Your browser does not support WebGL", [{
      text: "OK",
      callback: r
    }])
  },

修改后

compatibilityCheck: function(e, t, r) {
    UnityLoader.SystemInfo.hasWebGL ?  ["Edge", "Firefox", "Chrome", "Safari"].indexOf(UnityLoader.SystemInfo.browser) == -1 ? e.popup("Please note that your browser is not currently supported for this Unity WebGL content. Press OK if you wish to continue anyway.", [{
      text: "OK",
      callback: t
    }]) : t() : e.popup("Your browser does not support WebGL", [{
      text: "OK",
      callback: r
    }])
  },
全屏

index.html 修改如下: 在这里插入图片描述





  
  
  Unity WebGL Player | Pudong
  
  
  
  
  
    var gameInstance = UnityLoader.instantiate("gameContainer", "Build/build.json", {
      onProgress: UnityProgress
    });
  
  
  
    body {
      overflow: scroll;
      overflow-x: hidden;
      overflow-y: hidden;
    }
  

  
  
    function ChangeCanvas() {
      document.getElementById("gameContainer").style.width = window.innerWidth + "px";
      document.getElementById("gameContainer").style.height = window.innerHeight + "px";
      document.getElementById("#canvas").style.width = window.innerWidth + "px";
      document.getElementById("#canvas").style.height = window.innerHeight + "px";
    }
  



  
  

  




以上修改只能做到页面最大化,如果要全屏可以手动F11或者添加其他代码 在这里插入图片描述

加载时间过长

1.使用Gzip压缩格式 在这里插入图片描述 在这里插入图片描述

页面模板

模板demo 在这里插入图片描述

只要进度条不要Logo

用以下内容替换UnityProgress.js

function UnityProgress(gameInstance, progress) {
  if (!gameInstance.Module)
    return;

  if (!gameInstance.progress) {
    gameInstance.progress = document.createElement("div");
    gameInstance.progress.className = "progress " + gameInstance.Module.splashScreenStyle;
    gameInstance.progress.empty = document.createElement("div");
    gameInstance.progress.empty.className = "empty";
    gameInstance.progress.appendChild(gameInstance.progress.empty);
    gameInstance.progress.full = document.createElement("div");
    gameInstance.progress.full.className = "full";
    gameInstance.progress.appendChild(gameInstance.progress.full);
    gameInstance.container.appendChild(gameInstance.progress);
  }
  gameInstance.progress.full.style.width = (100 * progress) + "%";
  gameInstance.progress.empty.style.width = (100 * (1 - progress)) + "%";
  if (progress == 1)
    gameInstance.progress.style.display = "none";
}

Unity发布WebGL时如何修改、删除默认的载入进度条

自定义加载背景图

如下所示把黑色的背景换成了图片!!! 在这里插入图片描述 方法概况:直接把Logo换成想要的背景图,然后在css中修改尺寸即可{(>A

关注
打赏
1664520285
查看更多评论
0.5385s