当前位置: 首页 > news >正文

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_WEBGLLogUtil.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 fileadd_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 fileadd_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 fileadd_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 fileadd_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 fileadd_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 插件的办法

http://www.lryc.cn/news/24674.html

相关文章:

  • 基于部标JT808的车载视频监控需求与EasyCVR视频融合平台解决方案设计
  • Grafana邮件及告警配置
  • Springboot Java多线程操作本地文件,加读写锁,阻塞的线程等待运行中的线程执行完再查询并写入
  • WebRTC拥塞控制算法——GCC介绍
  • 大数据技术之Maxwell基础知识
  • 元数据管理实践数据血缘
  • SQL的优化【面试工作】
  • Kotlin 40. Dependency Injection 依赖注入以及Hilt在Kotlin中的使用,系列3:Hilt 注释介绍及使用案例
  • 1000亿数据、30W级qps如何架构?来一个天花板案例
  • 人工智能及其应用(蔡自兴)期末复习
  • openpnp - configure - 矫正里程碑
  • JavaScript高级程序设计读书分享之8章——8.2创建对象
  • 关于Could not build wheels for opencv-python-headless, which is...报错的解决方案
  • 【K3s】第1篇 K3s入门级介绍及架构详解
  • Java学习--反射
  • 应用和迭代(名词解释)
  • HTMLCollection 和 NodeList 区别
  • fork()出来一个进程,这个进程的父进程是从哪来的?
  • 结构体内存对齐
  • 【C语言进阶】指针进阶
  • java:Class的isPrimitive方法使用
  • TCP 握手过程 三次 四次
  • windows 下 安裝mysql 5.7.41 (64位) 超简单方式
  • 二叉树——二叉树的最近公共祖先
  • 数据结构与算法基础-学习-14-线性表之串
  • Mac 快捷键
  • 【微服务】-微服务环境搭建
  • IGKBoard(imx6ull)-ADC编程MQ-2烟雾传感器采样
  • 前端二面vue面试题总结
  • 时间API在更新,传奇已经谢幕,但技术永远不死