unity-web端h5记录
Posted 蝶泳奈何桥.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity-web端h5记录相关的知识,希望对你有一定的参考价值。
title: unity-web端h5记录
categories: Unity3d
tags: [unity, web, h5]
date: 2023-02-23 17:00:53
comments: false
mathjax: true
toc: true
unity-web端h5记录
前篇
- 5款常用的html5游戏引擎以及优缺点分析 - https://imgtec.eetrend.com/blog/2022/100557792.html
- Unity WebGL 开发指北(完全篇) - https://zhuanlan.zhihu.com/p/475307249
平台适配
文件 io 适配
- web 请求资源, 全都需要异步加载
构建和运行 WebGL 项目
- 构建和运行 WebGL 项目 - https://docs.unity3d.com/cn/2021.2/Manual/webgl-building.html
使用 nginx 作为文件服务器
- 下载地址: http://nginx.org/en/download.html
使用
-
启动
C:\\server\\nginx-1.0.2>start nginx or C:\\server\\nginx-1.0.2>nginx.exe
-
停止
C:\\server\\nginx-1.0.2>nginx.exe -s stop 或 C:\\server\\nginx-1.0.2>nginx.exe -s quit
-
重新载入配置
C:\\server\\nginx-1.0.2>nginx.exe -s reload
-
重新打开日志文件
C:\\server\\nginx-1.0.2>nginx.exe -s reopen
-
查看版本
C:\\server\\nginx-1.0.2>nginx -v
平台判断
-
编译宏 https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
#if UNITY_WEBGL LogUtil.D("--- UNITY_WEBGL "); #endif
-
运行时
Application.platform == RuntimePlatform.WebGLPlayer
http
使用 best http, 版本最新即可, 如: 2.8.2
websocket
使用 best http, 版本最新即可, 如: 2.8.2
- Unity下使用BestHTTP插件进行Http和WebSocket通信 - https://blog.csdn.net/foupwang/article/details/104725423
踩坑
未开启 gz
-
报错
Unable to parse Build/aaa.framework.js.gz! This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: gzip" present. Check browser Console and Devtools Network tab to debug.
官方推荐的是 使用 nginx, 参考: https://docs.unity3d.com/cn/2021.2/Manual/webgl-server-configuration-code-samples.html
-
在
http # 增加一个 server 配置 server listen 8811; server_name localhost; root C:/Users/wilker/Desktop/aaa; # unity 打出的 h5 包 index index.html # location / # root html; # index index.html index.htm; # error_page 500 502 503 504 /50x.html; location = /50x.html root html; ######################################## # Add the following config within http server configuration # ... # On-disk Brotli-precompressed data files should be served with compression enabled: location ~ .+\\.(data|symbols\\.json)\\.br$ # Because this file is already pre-compressed on disk, disable the on-demand compression on it. # Otherwise nginx would attempt double compression. gzip off; add_header Content-Encoding br; default_type application/octet-stream; # On-disk Brotli-precompressed javascript code files: location ~ .+\\.js\\.br$ gzip off; # Do not attempt dynamic gzip compression on an already compressed file add_header Content-Encoding br; default_type application/javascript; # On-disk Brotli-precompressed WebAssembly files: location ~ .+\\.wasm\\.br$ gzip off; # Do not attempt dynamic gzip compression on an already compressed file add_header Content-Encoding br; # Enable streaming WebAssembly compilation by specifying the correct MIME type for # Wasm files. default_type application/wasm; # On-disk gzip-precompressed data files should be served with compression enabled: location ~ .+\\.(data|symbols\\.json)\\.gz$ gzip off; # Do not attempt dynamic gzip compression on an already compressed file add_header Content-Encoding gzip; default_type application/octet-stream; # On-disk gzip-precompressed JavaScript code files: location ~ .+\\.js\\.gz$ gzip off; # Do not attempt dynamic gzip compression on an already compressed file add_header Content-Encoding gzip; default_type application/javascript; # On-disk gzip-precompressed WebAssembly files: location ~ .+\\.wasm\\.gz$ gzip off; # Do not attempt dynamic gzip compression on an already compressed file add_header Content-Encoding gzip; # Enable streaming WebAssembly compilation by specifying the correct MIME type for # Wasm files. default_type application/wasm;
移动设备不支持
-
报错
WebGL builds are notsupported on mobile devices.
据说不用管这个
中文显示
- a
http 请求
原生端旧的 best http 插件报错
Invoking error handler due to
ReferenceError: Runtime is not defined
at XMLHttpRequest.http_onload (http://localhost:8811/Build/aaa.framework.js.gz:3:85864)
- 解决办法: 升级 best http 插件到 2.8.2+ 即可.
UnityWebRequest 请求报错
exception thrown: TypeError: Cannot set properties of undefined (setting '1'),TypeError: Cannot set properties of undefined (setting '1')
at _JS_WebRequest_Create (http://localhost:8811/Build/slotsweb.framework.js.gz:3:73755)
at http://localhost:8811/Build/slotsweb.wasm.gz:wasm-function[47545]:0x10870bf
- 参考: https://forum.unity.com/threads/cannot-set-properties-of-undefined-setting-1-when-running-a-unitywebrequest.1248445/
- 暂时不去解决, 采用升级 best http 插件的办法
以上是关于unity-web端h5记录的主要内容,如果未能解决你的问题,请参考以下文章